Search in sources :

Example 6 with State

use of org.apache.mesos.state.State in project jesos by groupon.

the class AbstractTestState method testOldUpdateRefused.

@Test
public void testOldUpdateRefused() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final byte[] newValue = "Ich esse Autos zum Abendbrot und mache Kopfsprung ins Sandbecken. Gruen. Rot. Pferderennen.".getBytes(StandardCharsets.UTF_8);
    final Variable var = state.fetch("someValue").get();
    assertTrue(var.value().length == 0);
    JVariable storedVar = (JVariable) state.store(var.mutate(value)).get();
    storedVar = (JVariable) state.store(var.mutate(newValue)).get();
    assertNull(storedVar);
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) Test(org.junit.Test)

Example 7 with State

use of org.apache.mesos.state.State in project jesos by groupon.

the class AbstractTestState method testTenMonkeysPressTenKeys.

@Test
public void testTenMonkeysPressTenKeys() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final byte[] newValue = "Ich esse Autos zum Abendbrot und mache Kopfsprung ins Sandbecken. Gruen. Rot. Pferderennen.".getBytes(StandardCharsets.UTF_8);
    final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    final ImmutableList.Builder<ListenableFuture<String>> builder = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        builder.add(executor.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                final String key = "key-" + UUID.randomUUID().toString();
                final Variable var = state.fetch(key).get();
                assertTrue(var.value().length == 0);
                JVariable storedVar = (JVariable) state.store(var.mutate(value)).get();
                storedVar = (JVariable) state.store(storedVar.mutate(newValue)).get();
                assertNotNull(storedVar);
                final JVariable retrievedVar = (JVariable) state.fetch(key).get();
                assertNotNull(retrievedVar);
                assertArrayEquals(storedVar.value(), retrievedVar.value());
                assertEquals(storedVar.getName(), retrievedVar.getName());
                assertEquals(storedVar.getUuid(), retrievedVar.getUuid());
                return key;
            }
        }));
    }
    final List<String> keys = Futures.allAsList(builder.build()).get();
    for (final String key : keys) {
        final JVariable retrievedVar = (JVariable) state.fetch(key).get();
        assertNotNull(retrievedVar);
        assertArrayEquals(newValue, retrievedVar.value());
    }
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.DAYS);
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) ImmutableList(com.google.common.collect.ImmutableList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 8 with State

use of org.apache.mesos.state.State in project jesos by groupon.

the class AbstractTestState method testTenMonkeysHammerOnTenKeys.

@Test
public void testTenMonkeysHammerOnTenKeys() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final byte[] newValue = "Ich esse Autos zum Abendbrot und mache Kopfsprung ins Sandbecken. Gruen. Rot. Pferderennen.".getBytes(StandardCharsets.UTF_8);
    final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    final ImmutableList.Builder<Variable> varBuilder = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        final String key = "key-" + UUID.randomUUID().toString();
        final Variable var = state.fetch(key).get();
        assertTrue(var.value().length == 0);
        final Variable storedVar = state.store(var.mutate(value)).get();
        assertNotNull(storedVar);
        builder.add(key);
        varBuilder.add(storedVar);
    }
    final Set<String> keys = builder.build();
    final List<Variable> variables = varBuilder.build();
    final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    final ImmutableList.Builder<ListenableFuture<Integer>> resultBuilder = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        resultBuilder.add(executor.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                final ArrayList<Variable> vars = new ArrayList<>(variables);
                Collections.shuffle(vars);
                int updateCount = 0;
                for (final Variable var : vars) {
                    final Variable storedVar = state.store(var.mutate(newValue)).get();
                    if (storedVar != null) {
                        updateCount++;
                        Thread.sleep(2L);
                    }
                }
                return updateCount;
            }
        }));
    }
    final List<Integer> results = Futures.allAsList(resultBuilder.build()).get();
    int finalTally = 0;
    for (final Integer result : results) {
        finalTally += result;
    }
    assertEquals(10, finalTally);
    for (final String key : keys) {
        final Variable retrievedVar = state.fetch(key).get();
        assertNotNull(retrievedVar);
        assertArrayEquals(newValue, retrievedVar.value());
    }
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.DAYS);
}
Also used : Variable(org.apache.mesos.state.Variable) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ImmutableSet(com.google.common.collect.ImmutableSet) State(org.apache.mesos.state.State) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Test(org.junit.Test)

Example 9 with State

use of org.apache.mesos.state.State in project cassandra-mesos-deprecated by mesosphere.

the class PersistedCassandraFrameworkConfigurationTest method generatingNewConfigAllowsFrameworkNameAndClusterNameToBeDifferent.

@Test
public void generatingNewConfigAllowsFrameworkNameAndClusterNameToBeDifferent() throws Exception {
    final State state = new InMemoryState();
    final PersistedCassandraFrameworkConfiguration configuration = new PersistedCassandraFrameworkConfiguration(state, "cassandra.frameworkName", 15, 15, "2.1.4", 1.0, 2048, 2048, 1024, 1, 1, "*", "./backup", ".", true, true, "rack1", "dc1", Collections.<ExternalDc>emptyList(), "clusterName");
    assertThat(configuration.frameworkName()).isEqualTo("cassandra.frameworkName");
    assertThat(configuration.clusterName()).isEqualTo("clusterName");
}
Also used : InMemoryState(org.apache.mesos.state.InMemoryState) State(org.apache.mesos.state.State) InMemoryState(org.apache.mesos.state.InMemoryState) Test(org.junit.Test)

Example 10 with State

use of org.apache.mesos.state.State in project cassandra-mesos-deprecated by mesosphere.

the class Main method _main.

private static int _main() throws UnknownHostException {
    final Optional<String> portOption = Env.option("PORT0");
    if (!portOption.isPresent()) {
        throw new SystemExitException("Environment variable PORT0 must be defined", 5);
    }
    final int port0 = Integer.parseInt(portOption.get());
    final String host = Env.option("HOST").or("localhost");
    final Optional<String> clusterNameOpt = Env.option("CASSANDRA_CLUSTER_NAME");
    final int executorCount = Integer.parseInt(Env.option("CASSANDRA_NODE_COUNT").or("3"));
    final int seedCount = Integer.parseInt(Env.option("CASSANDRA_SEED_COUNT").or("2"));
    final double resourceCpuCores = Double.parseDouble(Env.option("CASSANDRA_RESOURCE_CPU_CORES").or("2.0"));
    final long resourceMemoryMegabytes = Long.parseLong(Env.option("CASSANDRA_RESOURCE_MEM_MB").or("2048"));
    final long resourceDiskMegabytes = Long.parseLong(Env.option("CASSANDRA_RESOURCE_DISK_MB").or("2048"));
    final long javaHeapMb = Long.parseLong(Env.option("CASSANDRA_RESOURCE_HEAP_MB").or("0"));
    final long healthCheckIntervalSec = Long.parseLong(Env.option("CASSANDRA_HEALTH_CHECK_INTERVAL_SECONDS").or("60"));
    final long bootstrapGraceTimeSec = Long.parseLong(Env.option("CASSANDRA_BOOTSTRAP_GRACE_TIME_SECONDS").or("120"));
    final String cassandraVersion = "2.1.4";
    final String clusterName = clusterNameOpt.or("cassandra");
    final String frameworkName = frameworkName(clusterNameOpt);
    final String zkUrl = Env.option("CASSANDRA_ZK").or("zk://localhost:2181/cassandra-mesos");
    final long zkTimeoutMs = Long.parseLong(Env.option("CASSANDRA_ZK_TIMEOUT_MS").or("10000"));
    final String mesosMasterZkUrl = Env.option("MESOS_ZK").or("zk://localhost:2181/mesos");
    final String mesosUser = Env.option("MESOS_USER").or("");
    final long failoverTimeout = Long.parseLong(Env.option("CASSANDRA_FAILOVER_TIMEOUT_SECONDS").or(String.valueOf(Period.days(7).toStandardSeconds().getSeconds())));
    final String mesosRole = Env.option("CASSANDRA_FRAMEWORK_MESOS_ROLE").or("*");
    // TODO: Temporary. Will be removed when MESOS-1554 is released
    final String dataDirectory = Env.option("CASSANDRA_DATA_DIRECTORY").or(DEFAULT_DATA_DIRECTORY);
    final String backupDirectory = Env.option("CASSANDRA_BACKUP_DIRECTORY").or(DEFAULT_BACKUP_DIRECTORY);
    final boolean jmxLocal = Boolean.parseBoolean(Env.option("CASSANDRA_JMX_LOCAL").or("true"));
    final boolean jmxNoAuthentication = Boolean.parseBoolean(Env.option("CASSANDRA_JMX_NO_AUTHENTICATION").or("false"));
    final String defaultRack = Env.option("CASSANDRA_DEFAULT_RACK").or("RAC1");
    final String defaultDc = Env.option("CASSANDRA_DEFAULT_DC").or("DC1");
    final List<ExternalDc> externalDcs = getExternalDcs(Env.filterStartsWith("CASSANDRA_EXTERNAL_DC_", true));
    final Matcher matcher = validateZkUrl(zkUrl);
    final State state = new ZooKeeperState(matcher.group(1), zkTimeoutMs, TimeUnit.MILLISECONDS, matcher.group(2));
    if (seedCount > executorCount || seedCount <= 0 || executorCount <= 0) {
        throw new IllegalArgumentException("number of nodes (" + executorCount + ") and/or number of seeds (" + seedCount + ") invalid");
    }
    final PersistedCassandraFrameworkConfiguration configuration = new PersistedCassandraFrameworkConfiguration(state, frameworkName, healthCheckIntervalSec, bootstrapGraceTimeSec, cassandraVersion, resourceCpuCores, resourceDiskMegabytes, resourceMemoryMegabytes, javaHeapMb, executorCount, seedCount, mesosRole, backupDirectory, dataDirectory, jmxLocal, jmxNoAuthentication, defaultRack, defaultDc, externalDcs, clusterName);
    final FrameworkInfo.Builder frameworkBuilder = FrameworkInfo.newBuilder().setFailoverTimeout(failoverTimeout).setUser(mesosUser).setName(frameworkName).setRole(mesosRole).setCheckpoint(true);
    final Optional<String> frameworkId = configuration.frameworkId();
    if (frameworkId.isPresent()) {
        frameworkBuilder.setId(frameworkId(frameworkId.get()));
    }
    final URI httpServerBaseUri = URI.create("http://" + host + ":" + port0 + "/");
    final Clock clock = new SystemClock();
    final PersistedCassandraClusterHealthCheckHistory healthCheckHistory = new PersistedCassandraClusterHealthCheckHistory(state);
    final PersistedCassandraClusterState clusterState = new PersistedCassandraClusterState(state);
    final SeedManager seedManager = new SeedManager(configuration, new ObjectMapper(), new SystemClock());
    final CassandraCluster cassandraCluster = new CassandraCluster(clock, httpServerBaseUri.toString(), new ExecutorCounter(state, 0L), clusterState, healthCheckHistory, new PersistedCassandraClusterJobs(state), configuration, seedManager);
    final HealthReportService healthReportService = new HealthReportService(clusterState, configuration, healthCheckHistory, clock);
    final Scheduler scheduler = new CassandraScheduler(configuration, cassandraCluster, clock);
    final JsonFactory factory = new JsonFactory();
    final ObjectMapper objectMapper = new ObjectMapper(factory);
    objectMapper.registerModule(new GuavaModule());
    // create JsonProvider to provide custom ObjectMapper
    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    provider.setMapper(objectMapper);
    final ResourceConfig rc = new ResourceConfig().registerInstances(new FileResourceController(cassandraVersion), new ApiController(factory), new ClusterCleanupController(cassandraCluster, factory), new ClusterRepairController(cassandraCluster, factory), new ClusterRollingRestartController(cassandraCluster, factory), new ClusterBackupController(cassandraCluster, factory), new ClusterRestoreController(cassandraCluster, factory), new ConfigController(cassandraCluster, factory), new LiveEndpointsController(cassandraCluster, factory), new NodeController(cassandraCluster, factory), new HealthCheckController(healthReportService), new QaReportController(cassandraCluster, factory), new ScaleOutController(cassandraCluster, factory), provider);
    final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(httpServerBaseUri, rc);
    final MesosSchedulerDriver driver;
    final Optional<Credential> credentials = getCredential();
    if (credentials.isPresent()) {
        frameworkBuilder.setPrincipal(credentials.get().getPrincipal());
        driver = new MesosSchedulerDriver(scheduler, frameworkBuilder.build(), mesosMasterZkUrl, credentials.get());
    } else {
        frameworkBuilder.setPrincipal("cassandra-framework");
        driver = new MesosSchedulerDriver(scheduler, frameworkBuilder.build(), mesosMasterZkUrl);
    }
    seedManager.startSyncingSeeds(60);
    final int status;
    switch(driver.run()) {
        case DRIVER_STOPPED:
            status = 0;
            break;
        case DRIVER_ABORTED:
            status = 1;
            break;
        case DRIVER_NOT_STARTED:
            status = 2;
            break;
        default:
            status = 3;
            break;
    }
    httpServer.shutdownNow();
    // Ensure that the driver process terminates.
    driver.stop(true);
    return status;
}
Also used : Clock(io.mesosphere.mesos.util.Clock) SystemClock(io.mesosphere.mesos.util.SystemClock) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) HealthReportService(io.mesosphere.mesos.frameworks.cassandra.scheduler.health.HealthReportService) MesosSchedulerDriver(org.apache.mesos.MesosSchedulerDriver) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SystemClock(io.mesosphere.mesos.util.SystemClock) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) State(org.apache.mesos.state.State) ZooKeeperState(org.apache.mesos.state.ZooKeeperState) Matcher(java.util.regex.Matcher) FrameworkInfo(org.apache.mesos.Protos.FrameworkInfo) Scheduler(org.apache.mesos.Scheduler) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ExternalDc(io.mesosphere.mesos.frameworks.cassandra.CassandraFrameworkProtos.ExternalDc) URI(java.net.URI) ZooKeeperState(org.apache.mesos.state.ZooKeeperState) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Credential(org.apache.mesos.Protos.Credential)

Aggregations

State (org.apache.mesos.state.State)16 Test (org.junit.Test)14 Variable (org.apache.mesos.state.Variable)11 InMemoryState (org.apache.mesos.state.InMemoryState)5 ImmutableList (com.google.common.collect.ImmutableList)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)2 CassandraFrameworkProtos (io.mesosphere.mesos.frameworks.cassandra.CassandraFrameworkProtos)2 Callable (java.util.concurrent.Callable)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)1 JacksonJaxbJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 ExternalDc (io.mesosphere.mesos.frameworks.cassandra.CassandraFrameworkProtos.ExternalDc)1 HealthReportService (io.mesosphere.mesos.frameworks.cassandra.scheduler.health.HealthReportService)1 Clock (io.mesosphere.mesos.util.Clock)1 SystemClock (io.mesosphere.mesos.util.SystemClock)1 URI (java.net.URI)1