Skip to content

Instantly share code, notes, and snippets.

@lin-zhao
Last active January 4, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lin-zhao/8544495 to your computer and use it in GitHub Desktop.
Save lin-zhao/8544495 to your computer and use it in GitHub Desktop.
##### Heap utilization statistics [MB] #####
Used Memory:542
Free Memory:895
Total Memory:1437
Max Memory:1437
Executor sutting down!
#!/usr/bin/env bash
UBER_EXAMPLES_JAR=/home/lin/uber-mesos-groupon-1.0-SNAPSHOT.jar
exec ${JAVA} -cp ${UBER_EXAMPLES_JAR} -Xmx1500m -Xms1500m \
com.groupon.mesos.examples.TestExecutor "${@}"
package com.groupon.mesos.examples;
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.util.Arrays;
import org.apache.mesos.*;
import org.apache.mesos.Protos.*;
public class TestExecutor implements Executor {
@Override
public void registered(ExecutorDriver driver,
ExecutorInfo executorInfo,
FrameworkInfo frameworkInfo,
SlaveInfo slaveInfo) {
System.out.println("Registered executor on " + slaveInfo.getHostname());
}
@Override
public void reregistered(ExecutorDriver driver, SlaveInfo executorInfo) {
}
@Override
public void disconnected(ExecutorDriver driver) {
}
//Launches a cpu intensive thread;
private void launchForeverLoop() {
new Thread() {
public void run() {
int i = 0;
while(i < 1) {
}
}
}.start();
}
@Override
public void launchTask(final ExecutorDriver driver, final TaskInfo task) {
new Thread() {
public void run() {
try {
TaskStatus status = TaskStatus.newBuilder()
.setTaskId(task.getTaskId())
.setState(TaskState.TASK_RUNNING).build();
driver.sendStatusUpdate(status);
System.out.println("Running task " + task.getTaskId());
// This is where one would perform the requested task.
//Getting the runtime reference from system
Runtime runtime = Runtime.getRuntime();
System.out.println("##### Heap utilization statistics [MB] #####");
int mb = 1024 * 1024;
byte[] data = new byte[512 * mb];
//Print used memory
System.out.println("Used Memory:"
+ (runtime.totalMemory() - runtime.freeMemory()) / mb);
//Print free memory
System.out.println("Free Memory:"
+ runtime.freeMemory() / mb);
//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);
//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);
System.out.println("Launching forever loops.");
launchForeverLoop();
launchForeverLoop();
launchForeverLoop();
System.out.println("Sleeping.");
Thread.sleep(300000);
status = TaskStatus.newBuilder()
.setTaskId(task.getTaskId())
.setState(TaskState.TASK_FINISHED).build();
driver.sendStatusUpdate(status);
} catch (Exception e) {
e.printStackTrace();
TaskStatus status = TaskStatus.newBuilder()
.setTaskId(task.getTaskId())
.setState(TaskState.TASK_FAILED).build();
driver.sendStatusUpdate(status);
}
}
}.start();
}
@Override
public void killTask(ExecutorDriver driver, TaskID taskId) {
System.out.println("Executor killed! taskID=" + taskId);
}
@Override
public void frameworkMessage(ExecutorDriver driver, byte[] data) {
System.out.println("Framework msg: " + Arrays.toString(data));
}
@Override
public void shutdown(ExecutorDriver driver) {
System.out.println("Executor sutting down!");
}
@Override
public void error(ExecutorDriver driver, String message) {
System.out.println("Executor error! msg=" + message);
}
public static void main(String[] args) throws Exception {
MesosExecutorDriver driver = new MesosExecutorDriver(new TestExecutor());
System.exit(driver.run() == Status.DRIVER_STOPPED ? 0 : 1);
}
}
package com.groupon.mesos.examples;
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.protobuf.ByteString;
import org.apache.mesos.*;
import org.apache.mesos.Protos.*;
public class TestFramework {
static class TestScheduler implements Scheduler {
public TestScheduler(ExecutorInfo executor) {
this(executor, 3);
}
public TestScheduler(ExecutorInfo executor, int totalTasks) {
this.executor = executor;
this.totalTasks = totalTasks;
}
@Override
public void registered(SchedulerDriver driver,
FrameworkID frameworkId,
MasterInfo masterInfo) {
System.out.println("Registered! ID = " + frameworkId.getValue());
}
@Override
public void reregistered(SchedulerDriver driver, MasterInfo masterInfo) {
}
@Override
public void disconnected(SchedulerDriver driver) {
}
@Override
public void resourceOffers(SchedulerDriver driver,
List<Offer> offers) {
for (Offer offer : offers) {
System.out.println("Offer: " + offer.toString());
List<TaskInfo> tasks = new ArrayList<TaskInfo>();
if (launchedTasks < totalTasks) {
TaskID taskId = TaskID.newBuilder()
.setValue(Integer.toString(launchedTasks++)).build();
System.out.println("Launching task " + taskId.getValue());
TaskInfo task = TaskInfo.newBuilder()
.setName("task " + taskId.getValue())
.setTaskId(taskId)
.setSlaveId(offer.getSlaveId())
.addResources(Resource.newBuilder()
.setName("cpus")
.setType(Value.Type.SCALAR)
.setScalar(Value.Scalar.newBuilder().setValue(2)))
.addResources(Resource.newBuilder()
.setName("mem")
.setType(Value.Type.SCALAR)
.setScalar(Value.Scalar.newBuilder().setValue(128)))
.setExecutor(ExecutorInfo.newBuilder(executor))
.build();
tasks.add(task);
Filters filters = Filters.newBuilder().setRefuseSeconds(1).build();
driver.launchTasks(offer.getId(), tasks, filters);
} else {
driver.declineOffer(offer.getId());
}
}
}
@Override
public void offerRescinded(SchedulerDriver driver, OfferID offerId) {
}
@Override
public void statusUpdate(SchedulerDriver driver, TaskStatus status) {
System.out.println("Status update: task " + status.getTaskId().getValue() +
" is in state " + status.getState());
if (status.getState() == TaskState.TASK_FINISHED) {
finishedTasks++;
System.out.println("Finished tasks: " + finishedTasks);
if (finishedTasks == totalTasks) {
driver.stop();
}
}
}
@Override
public void frameworkMessage(SchedulerDriver driver,
ExecutorID executorId,
SlaveID slaveId,
byte[] data) {
}
@Override
public void slaveLost(SchedulerDriver driver, SlaveID slaveId) {
}
@Override
public void executorLost(SchedulerDriver driver,
ExecutorID executorId,
SlaveID slaveId,
int status) {
}
public void error(SchedulerDriver driver, String message) {
System.out.println("Error: " + message);
}
private final ExecutorInfo executor;
private final int totalTasks;
private int launchedTasks = 0;
private int finishedTasks = 0;
}
private static void usage() {
String name = TestFramework.class.getName();
System.err.println("Usage: " + name + " master <tasks>");
}
public static void main(String[] args) throws Exception {
if (args.length < 1 || args.length > 2) {
usage();
System.exit(1);
}
String uri = new File("./test-executor").getCanonicalPath();
ExecutorInfo executor = ExecutorInfo.newBuilder()
.setExecutorId(ExecutorID.newBuilder().setValue("default"))
.setCommand(CommandInfo.newBuilder().setValue(uri))
.setName("Test Executor (Java)")
.setSource("java_test")
.build();
FrameworkInfo.Builder frameworkBuilder = FrameworkInfo.newBuilder()
.setUser("") // Have Mesos fill in the current user.
.setName("Test Framework (Java)");
// TODO(vinod): Make checkpointing the default when it is default
// on the slave.
if (System.getenv("MESOS_CHECKPOINT") != null) {
System.out.println("Enabling checkpoint for the framework");
frameworkBuilder.setCheckpoint(true);
}
FrameworkInfo framework = frameworkBuilder.build();
Scheduler scheduler = args.length == 1
? new TestScheduler(executor)
: new TestScheduler(executor, Integer.parseInt(args[1]));
MesosSchedulerDriver driver = null;
System.out.println("Loading driver.");
driver = new MesosSchedulerDriver(scheduler, framework, args[0]);
System.out.println("Running driver");
int status = driver.run() == Status.DRIVER_STOPPED ? 0 : 1;
System.out.println("Done running driver");
// Ensure that the driver process terminates.
driver.stop();
System.out.println("Stopped driver");
System.exit(status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment