Search in sources :

Example 16 with SocketStoreClientFactory

use of voldemort.client.SocketStoreClientFactory in project voldemort by voldemort.

the class ClientExample method avroStoreExample.

public static void avroStoreExample() {
    System.out.println("==============Avro store example=================");
    // In production environment, the StoreClient instantiation should be done using factory pattern
    // through a Framework such as Spring
    String bootstrapUrl = "tcp://localhost:6666";
    StoreClientFactory factory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(bootstrapUrl));
    StoreClient<GenericRecord, GenericRecord> client = factory.getStoreClient("avro-example");
    // creating initial k-v pair
    System.out.println("Creating initial Key and Value");
    String keySchemaJson = "{ \"name\": \"key\", \"type\": \"record\", \"fields\": [{ \"name\": \"user_id\", \"type\": \"int\" }] }";
    Schema keySchema = Schema.parse(keySchemaJson);
    GenericRecord key = new GenericData.Record(keySchema);
    key.put("user_id", 123);
    String valueSchemaJson = "{\n" + "      \"name\": \"value\",\n" + "      \"type\": \"record\",\n" + "      \"fields\": [{ \n" + "        \"name\": \"user_id\",\n" + "        \"type\": \"int\"\n" + "      }, {\n" + "        \"name\": \"gender\",\n" + "        \"type\": \"string\"\n" + "      }, {\n" + "        \"name\": \"age\",\n" + "        \"type\": \"int\",\n" + "        \"optional\": true\n" + "      }]\n" + "    }";
    Schema valueSchema = Schema.parse(valueSchemaJson);
    GenericRecord value = new GenericData.Record(valueSchema);
    value.put("user_id", 123);
    value.put("gender", "male");
    value.put("age", 23);
    // put initial value
    System.out.println("Putting Initial value");
    client.put(key, value);
    // get the value
    System.out.println("Getting the value");
    Versioned<GenericRecord> versioned = client.get(key);
    System.out.println("Initial Versioned Object: " + String.valueOf(versioned));
    System.out.println("           Initial Value: " + String.valueOf(versioned.getValue()));
    // modify the value
    System.out.println("Modifying the value");
    GenericRecord modifiedRecord = versioned.getValue();
    modifiedRecord.put("gender", "female");
    modifiedRecord.put("age", 55);
    versioned.setObject(modifiedRecord);
    // update the value
    System.out.println("Putting the new value");
    client.put(key, versioned);
    // get again and print
    System.out.println("Getting the new value");
    versioned = client.get(key);
    System.out.println("    New Versioned Object: " + String.valueOf(versioned));
    System.out.println("               New Value: " + String.valueOf(versioned.getValue()));
}
Also used : SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) ClientConfig(voldemort.client.ClientConfig) GenericRecord(org.apache.avro.generic.GenericRecord) SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) StoreClientFactory(voldemort.client.StoreClientFactory)

Example 17 with SocketStoreClientFactory

use of voldemort.client.SocketStoreClientFactory in project voldemort by voldemort.

the class RedirectingStoreTest method setUp.

@Before
public void setUp() throws IOException, InterruptedException {
    currentCluster = ServerTestUtils.getLocalCluster(3, new int[][] { { 0, 1 }, { 2, 3 }, {} });
    targetCluster = UpdateClusterUtils.createUpdatedCluster(currentCluster, 2, Arrays.asList(0));
    this.primaryPartitionsMoved = Lists.newArrayList(0);
    this.secondaryPartitionsMoved = Lists.newArrayList(2, 3);
    this.storeDef = new StoreDefinitionBuilder().setName("test").setType(BdbStorageConfiguration.TYPE_NAME).setKeySerializer(new SerializerDefinition("string")).setValueSerializer(new SerializerDefinition("string")).setRoutingPolicy(RoutingTier.CLIENT).setRoutingStrategyType(RoutingStrategyType.CONSISTENT_STRATEGY).setReplicationFactor(2).setPreferredReads(1).setRequiredReads(1).setPreferredWrites(1).setRequiredWrites(1).build();
    File tempStoreXml = new File(TestUtils.createTempDir(), "stores.xml");
    FileUtils.writeStringToFile(tempStoreXml, new StoreDefinitionsMapper().writeStoreList(Lists.newArrayList(storeDef)));
    this.servers = new VoldemortServer[3];
    for (int nodeId = 0; nodeId < 3; nodeId++) {
        this.servers[nodeId] = startServer(nodeId, tempStoreXml.getAbsolutePath(), currentCluster);
    }
    // Start another node for only this unit test
    HashMap<ByteArray, byte[]> entrySet = ServerTestUtils.createRandomKeyValuePairs(100);
    SocketStoreClientFactory factory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(Lists.newArrayList("tcp://" + currentCluster.getNodeById(0).getHost() + ":" + currentCluster.getNodeById(0).getSocketPort())));
    StoreClient<Object, Object> storeClient = factory.getStoreClient("test");
    this.primaryEntriesMoved = Maps.newHashMap();
    this.secondaryEntriesMoved = Maps.newHashMap();
    this.proxyPutTestPrimaryEntries = Maps.newHashMap();
    this.proxyPutTestSecondaryEntries = Maps.newHashMap();
    RoutingStrategy strategy = new RoutingStrategyFactory().updateRoutingStrategy(storeDef, currentCluster);
    for (Entry<ByteArray, byte[]> entry : entrySet.entrySet()) {
        storeClient.put(new String(entry.getKey().get()), new String(entry.getValue()));
        List<Integer> pList = strategy.getPartitionList(entry.getKey().get());
        if (primaryPartitionsMoved.contains(pList.get(0))) {
            primaryEntriesMoved.put(entry.getKey(), entry.getValue());
        } else if (secondaryPartitionsMoved.contains(pList.get(0))) {
            secondaryEntriesMoved.put(entry.getKey(), entry.getValue());
        }
    }
    // Sleep a while for the queries to go through...
    // Hope the 'God of perfect timing' is on our side
    Thread.sleep(500);
    // steal a few primary key-value pairs for testing proxy put logic
    int cnt = 0;
    for (Entry<ByteArray, byte[]> entry : primaryEntriesMoved.entrySet()) {
        if (cnt > 3)
            break;
        this.proxyPutTestPrimaryEntries.put(entry.getKey(), entry.getValue());
        cnt++;
    }
    for (ByteArray key : this.proxyPutTestPrimaryEntries.keySet()) {
        this.primaryEntriesMoved.remove(key);
    }
    assertTrue("Not enough primary entries", primaryEntriesMoved.size() > 1);
    // steal a few secondary key-value pairs for testing proxy put logic
    cnt = 0;
    for (Entry<ByteArray, byte[]> entry : secondaryEntriesMoved.entrySet()) {
        if (cnt > 3)
            break;
        this.proxyPutTestSecondaryEntries.put(entry.getKey(), entry.getValue());
        cnt++;
    }
    for (ByteArray key : this.proxyPutTestSecondaryEntries.keySet()) {
        this.secondaryEntriesMoved.remove(key);
    }
    assertTrue("Not enough secondary entries", primaryEntriesMoved.size() > 1);
    RebalanceBatchPlan RebalanceBatchPlan = new RebalanceBatchPlan(currentCluster, targetCluster, Lists.newArrayList(storeDef));
    List<RebalanceTaskInfo> plans = Lists.newArrayList(RebalanceBatchPlan.getBatchPlan());
    // Set into rebalancing state
    for (RebalanceTaskInfo partitionPlan : plans) {
        servers[partitionPlan.getStealerId()].getMetadataStore().put(MetadataStore.SERVER_STATE_KEY, MetadataStore.VoldemortState.REBALANCING_MASTER_SERVER);
        servers[partitionPlan.getStealerId()].getMetadataStore().put(MetadataStore.REBALANCING_STEAL_INFO, new RebalancerState(Lists.newArrayList(partitionPlan)));
        servers[partitionPlan.getStealerId()].getMetadataStore().put(MetadataStore.REBALANCING_SOURCE_CLUSTER_XML, currentCluster);
        // update original storedefs
        servers[partitionPlan.getStealerId()].getMetadataStore().put(MetadataStore.REBALANCING_SOURCE_STORES_XML, Lists.newArrayList(storeDef));
    }
    // Update the cluster metadata on all three nodes
    for (VoldemortServer server : servers) {
        server.getMetadataStore().put(MetadataStore.CLUSTER_KEY, targetCluster);
    }
}
Also used : StoreDefinitionBuilder(voldemort.store.StoreDefinitionBuilder) RoutingStrategyFactory(voldemort.routing.RoutingStrategyFactory) StoreDefinitionsMapper(voldemort.xml.StoreDefinitionsMapper) VoldemortServer(voldemort.server.VoldemortServer) RebalanceBatchPlan(voldemort.client.rebalance.RebalanceBatchPlan) SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) RoutingStrategy(voldemort.routing.RoutingStrategy) ByteArray(voldemort.utils.ByteArray) RebalancerState(voldemort.server.rebalance.RebalancerState) ClientConfig(voldemort.client.ClientConfig) File(java.io.File) SerializerDefinition(voldemort.serialization.SerializerDefinition) RebalanceTaskInfo(voldemort.client.rebalance.RebalanceTaskInfo) Before(org.junit.Before)

Example 18 with SocketStoreClientFactory

use of voldemort.client.SocketStoreClientFactory in project voldemort by voldemort.

the class ZoneCountWriteTest method setup.

@Before
public void setup() throws IOException {
    storeDef = storeDefs.get(0);
    Integer zoneCountWrite = 1;
    // override
    storeDef = new StoreDefinition(storeDef.getName(), storeDef.getType(), storeDef.getDescription(), storeDef.getKeySerializer(), storeDef.getValueSerializer(), storeDef.getTransformsSerializer(), storeDef.getRoutingPolicy(), storeDef.getRoutingStrategyType(), storeDef.getReplicationFactor(), storeDef.getPreferredReads(), storeDef.getRequiredReads(), storeDef.getPreferredWrites(), storeDef.getRequiredWrites(), storeDef.getViewTargetStoreName(), storeDef.getValueTransformation(), storeDef.getZoneReplicationFactor(), storeDef.getZoneCountReads(), zoneCountWrite, storeDef.getRetentionDays(), storeDef.getRetentionScanThrottleRate(), storeDef.getRetentionFrequencyDays(), storeDef.getSerializerFactory(), storeDef.getHintedHandoffStrategyType(), storeDef.getHintPrefListSize(), storeDef.getOwners(), storeDef.getMemoryFootprintMB());
    storeDefs.set(0, storeDef);
    SocketStoreClientFactory socketStoreClientFactory = new SocketStoreClientFactory(clientConfig);
    for (Integer nodeId : cluster.getNodeIds()) {
        SocketStoreFactory socketStoreFactory = new ClientRequestExecutorPool(2, 10000, 100000, 1024);
        VoldemortConfig config = ServerTestUtils.createServerConfigWithDefs(true, nodeId, TestUtils.createTempDir().getAbsolutePath(), cluster, storeDefs, new Properties());
        VoldemortServer vs = ServerTestUtils.startVoldemortServer(socketStoreFactory, config, cluster);
        vservers.put(nodeId, vs);
    }
    client = socketStoreClientFactory.getRawStore(storeDef.getName(), null);
}
Also used : ClientRequestExecutorPool(voldemort.store.socket.clientrequest.ClientRequestExecutorPool) SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) StoreDefinition(voldemort.store.StoreDefinition) SocketStoreFactory(voldemort.store.socket.SocketStoreFactory) Properties(java.util.Properties) VoldemortServer(voldemort.server.VoldemortServer) VoldemortConfig(voldemort.server.VoldemortConfig) Before(org.junit.Before)

Example 19 with SocketStoreClientFactory

use of voldemort.client.SocketStoreClientFactory in project voldemort by voldemort.

the class SystemStoreTest method setUp.

@Before
public void setUp() throws Exception {
    servers = new VoldemortServer[cluster.getNodeIds().size()];
    int i = 0;
    for (Integer nodeId : cluster.getNodeIds()) {
        VoldemortConfig config = ServerTestUtils.createServerConfigWithDefs(true, nodeId, TestUtils.createTempDir().getAbsolutePath(), cluster, storeDefs, new Properties());
        VoldemortServer server = ServerTestUtils.startVoldemortServer(socketStoreFactory, config);
        servers[i++] = server;
    }
    socketUrl = servers[0].getIdentityNode().getSocketUrl().toString();
    ClientConfig clientConfig = new ClientConfig().setMaxTotalConnections(4).setMaxConnectionsPerNode(4).setBootstrapUrls(socketUrl);
    SocketStoreClientFactory socketFactory = new SocketStoreClientFactory(clientConfig);
    bootStrapUrls = new String[1];
    bootStrapUrls[0] = socketUrl;
    clusterXml = ((AbstractStoreClientFactory) socketFactory).bootstrapMetadataWithRetries(MetadataStore.CLUSTER_KEY);
}
Also used : SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) Properties(java.util.Properties) VoldemortServer(voldemort.server.VoldemortServer) ClientConfig(voldemort.client.ClientConfig) VoldemortConfig(voldemort.server.VoldemortConfig) Before(org.junit.Before)

Example 20 with SocketStoreClientFactory

use of voldemort.client.SocketStoreClientFactory in project voldemort by voldemort.

the class ChainedInconsistencyResolverTest method setUp.

@Before
public void setUp() throws IOException {
    boolean useNio = false;
    int numServers = 2;
    VoldemortServer[] servers = new VoldemortServer[numServers];
    Cluster cluster = ServerTestUtils.startVoldemortCluster(numServers, servers, null, socketStoreFactory, useNio, null, STORES_XML, new Properties());
    // Initialize versioned puts for basic test
    v1 = getVersioned(0, 1, 1, 1, 1, 1);
    v2 = getVersioned(0, 0, 1, 1, 1, 1);
    // Initialize versioned puts for > 1 conflicts
    conflict1 = getVersioned(0, 1, 1, 1, 1, 1, 1, 1, 1, 1);
    conflict2 = getVersioned(0, 0, 1, 1, 1, 1, 1, 1, 1, 1);
    conflict3 = getVersioned(0, 0, 0, 1, 1, 1, 1, 1, 1, 1);
    conflict4 = getVersioned(0, 0, 0, 0, 1, 1, 1, 1, 1, 1);
    conflict5 = getVersioned(0, 0, 0, 0, 0, 1, 1, 1, 1, 1);
    conflict6 = getVersioned(0, 0, 0, 0, 0, 0, 1, 1, 1, 1);
    Node node = cluster.getNodes().iterator().next();
    String bootstrapUrl = "tcp://" + node.getHost() + ":" + node.getSocketPort();
    StoreClientFactory storeClientFactory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(bootstrapUrl));
    defaultStoreClient = storeClientFactory.getStoreClient("test");
    socketStore = ServerTestUtils.getSocketStore(socketStoreFactory, "test", node.getSocketPort(), RequestFormatType.VOLDEMORT_V1);
}
Also used : SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) Node(voldemort.cluster.Node) Cluster(voldemort.cluster.Cluster) VoldemortServer(voldemort.server.VoldemortServer) Properties(java.util.Properties) ClientConfig(voldemort.client.ClientConfig) SocketStoreClientFactory(voldemort.client.SocketStoreClientFactory) StoreClientFactory(voldemort.client.StoreClientFactory) Before(org.junit.Before)

Aggregations

SocketStoreClientFactory (voldemort.client.SocketStoreClientFactory)40 ClientConfig (voldemort.client.ClientConfig)37 IOException (java.io.IOException)14 VoldemortServer (voldemort.server.VoldemortServer)14 ByteArray (voldemort.utils.ByteArray)13 Properties (java.util.Properties)12 Before (org.junit.Before)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)9 StoreClientFactory (voldemort.client.StoreClientFactory)9 Cluster (voldemort.cluster.Cluster)8 HashMap (java.util.HashMap)7 ExecutorService (java.util.concurrent.ExecutorService)7 Node (voldemort.cluster.Node)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ClusterTestUtils (voldemort.ClusterTestUtils)6 DefaultStoreClient (voldemort.client.DefaultStoreClient)6 InvalidMetadataException (voldemort.store.InvalidMetadataException)6