Search in sources :

Example 6 with Variable

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

the class JLevelDBState method store.

@Override
public Future<Variable> store(final Variable variable) {
    checkNotNull(variable, "variable is null");
    checkState(!closed.get(), "already closed");
    checkState(variable instanceof JVariable, "can not process native variable, use JVariable");
    final JVariable v = (JVariable) variable;
    return executor.submit(new Callable<Variable>() {

        @Override
        public Variable call() throws Exception {
            final WriteOptions writeOptions = new WriteOptions();
            writeOptions.sync(true);
            final String internedName = v.getName().intern();
            synchronized (internedName) {
                final JVariable current = load(internedName);
                if (current == null || current.getUuid().equals(v.getUuid())) {
                    final JVariable update = new JVariable(internedName, v.value());
                    final WriteBatch writeBatch = db.createWriteBatch();
                    writeBatch.delete(bytes(internedName));
                    writeBatch.put(bytes(internedName), update.getEntry().toByteArray());
                    db.write(writeBatch, writeOptions);
                    return update;
                } else {
                    return null;
                }
            }
        }
    });
}
Also used : WriteOptions(org.iq80.leveldb.WriteOptions) Variable(org.apache.mesos.state.Variable) Iq80DBFactory.asString(org.iq80.leveldb.impl.Iq80DBFactory.asString) WriteBatch(org.iq80.leveldb.WriteBatch) IOException(java.io.IOException)

Example 7 with Variable

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

the class AbstractTestState method testUpdateOk.

@Test
public void testUpdateOk() 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(storedVar.mutate(newValue)).get();
    assertNotNull(storedVar);
    final JVariable retrievedVar = (JVariable) state.fetch("someValue").get();
    assertNotNull(retrievedVar);
    assertArrayEquals(storedVar.value(), retrievedVar.value());
    assertEquals(storedVar.getName(), retrievedVar.getName());
    assertEquals(storedVar.getUuid(), retrievedVar.getUuid());
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) Test(org.junit.Test)

Example 8 with Variable

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

the class AbstractTestState method testNames.

@Test
public void testNames() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final ImmutableSortedSet.Builder<String> builder = ImmutableSortedSet.naturalOrder();
    for (int i = 0; i < 10; i++) {
        final String key = "name-" + UUID.randomUUID().toString();
        builder.add(key);
        final Variable var = state.fetch(key).get();
        assertTrue(var.value().length == 0);
        state.store(var.mutate(value)).get();
    }
    final SortedSet<String> keys = builder.build();
    final Iterator<String> it = state.names().get();
    final SortedSet<String> entries = ImmutableSortedSet.copyOf(it);
    assertEquals(keys, entries);
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Test(org.junit.Test)

Example 9 with Variable

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

the class AbstractTestState method testNonExistentValue.

@Test
public void testNonExistentValue() throws Exception {
    final State state = getState();
    final Variable empty = state.fetch("does-not-exist").get();
    assertNotNull(empty);
    assertTrue(empty.value().length == 0);
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) Test(org.junit.Test)

Example 10 with Variable

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

the class AbstractTestState method testOldExpungeRefused.

@Test
public void testOldExpungeRefused() throws Exception {
    final State state = getState();
    final byte[] value = "The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8);
    final Variable var = state.fetch("someValue").get();
    assertTrue(var.value().length == 0);
    final JVariable storedVar = (JVariable) state.store(var.mutate(value)).get();
    final boolean expunged = state.expunge(var).get();
    assertFalse(expunged);
    final JVariable retrievedVar = (JVariable) state.fetch("someValue").get();
    assertNotNull(retrievedVar);
    assertArrayEquals(storedVar.value(), retrievedVar.value());
    assertEquals(storedVar.getName(), retrievedVar.getName());
    assertEquals(storedVar.getUuid(), retrievedVar.getUuid());
}
Also used : Variable(org.apache.mesos.state.Variable) State(org.apache.mesos.state.State) Test(org.junit.Test)

Aggregations

Variable (org.apache.mesos.state.Variable)13 State (org.apache.mesos.state.State)11 Test (org.junit.Test)11 ImmutableList (com.google.common.collect.ImmutableList)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)2 Callable (java.util.concurrent.Callable)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NotNull (javax.validation.constraints.NotNull)1 InMemoryState (org.apache.mesos.state.InMemoryState)1 WriteBatch (org.iq80.leveldb.WriteBatch)1 WriteOptions (org.iq80.leveldb.WriteOptions)1 Iq80DBFactory.asString (org.iq80.leveldb.impl.Iq80DBFactory.asString)1