Search in sources :

Example 31 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class ElasticJoinProducer method fetchPerPartitionTxnId.

/*
     * Inherit the per partition txnid from the long since gone
     * partition that existed in the past
     */
private long[] fetchPerPartitionTxnId() {
    ZooKeeper zk = VoltDB.instance().getHostMessenger().getZK();
    byte[] partitionTxnIdsBytes = null;
    try {
        partitionTxnIdsBytes = zk.getData(VoltZK.perPartitionTxnIds, false, null);
    }//Can be no node if the cluster was never restored
     catch (KeeperException.NoNodeException e) {
        return null;
    } catch (Exception e) {
        VoltDB.crashLocalVoltDB("Error retrieving per partition txn ids", true, e);
    }
    ByteBuffer buf = ByteBuffer.wrap(partitionTxnIdsBytes);
    int count = buf.getInt();
    Long partitionTxnId = null;
    long[] partitionTxnIds = new long[count];
    for (int ii = 0; ii < count; ii++) {
        long txnId = buf.getLong();
        partitionTxnIds[ii] = txnId;
        int partitionId = TxnEgo.getPartitionId(txnId);
        if (partitionId == m_partitionId) {
            partitionTxnId = txnId;
            continue;
        }
    }
    if (partitionTxnId != null) {
        return partitionTxnIds;
    }
    return null;
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) ByteBuffer(java.nio.ByteBuffer) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 32 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestCartographer method testSPMasterChange.

@Test
public void testSPMasterChange() throws Exception {
    ZooKeeper zk = getClient(0);
    VoltZK.createPersistentZKNodes(zk);
    LeaderCache spwriter = new LeaderCache(zk, VoltZK.iv2masters);
    HostMessenger hm = mock(HostMessenger.class);
    when(hm.getZK()).thenReturn(m_messengers.get(0).getZK());
    Cartographer dut = new Cartographer(hm, 0, false);
    // Startup partitions
    spwriter.start(true);
    spwriter.put(0, 0l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    spwriter.put(1, 1l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    spwriter.put(2, 2l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    // now change master for part 0
    spwriter.put(0, 3l);
    ArgumentCaptor<Long> hsIdCaptor = ArgumentCaptor.forClass(Long.class);
    ArgumentCaptor<BinaryPayloadMessage> bpmCaptor = ArgumentCaptor.forClass(BinaryPayloadMessage.class);
    verify(hm, timeout(10000)).send(hsIdCaptor.capture(), bpmCaptor.capture());
    JSONObject jsObj = new JSONObject(new String(bpmCaptor.getValue().m_payload, "UTF-8"));
    System.out.println("BPM: " + jsObj.toString());
    final int partitionId = jsObj.getInt(Cartographer.JSON_PARTITION_ID);
    final long initiatorHSId = jsObj.getLong(Cartographer.JSON_INITIATOR_HSID);
    assertEquals(0, partitionId);
    assertEquals(3, initiatorHSId);
    spwriter.shutdown();
}
Also used : BinaryPayloadMessage(org.voltcore.messaging.BinaryPayloadMessage) VoltMessage(org.voltcore.messaging.VoltMessage) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) JSONObject(org.json_voltpatches.JSONObject) HostMessenger(org.voltcore.messaging.HostMessenger) Matchers.anyLong(org.mockito.Matchers.anyLong) Test(org.junit.Test)

Example 33 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestCartographer method testMPIChange.

@Test
public void testMPIChange() throws Exception {
    ZooKeeper zk = getClient(0);
    VoltZK.createPersistentZKNodes(zk);
    LeaderCache mpwriter = new LeaderCache(zk, VoltZK.iv2mpi);
    HostMessenger hm = mock(HostMessenger.class);
    when(hm.getZK()).thenReturn(m_messengers.get(0).getZK());
    Cartographer dut = new Cartographer(hm, 0, false);
    mpwriter.start(true);
    // initial master
    mpwriter.put(MpInitiator.MP_INIT_PID, 0l);
    verify(hm, timeout(10000)).send(anyLong(), any(VoltMessage.class));
    reset(hm);
    // Now change the master
    mpwriter.put(MpInitiator.MP_INIT_PID, 3l);
    ArgumentCaptor<Long> hsIdCaptor = ArgumentCaptor.forClass(Long.class);
    ArgumentCaptor<BinaryPayloadMessage> bpmCaptor = ArgumentCaptor.forClass(BinaryPayloadMessage.class);
    verify(hm, timeout(10000)).send(hsIdCaptor.capture(), bpmCaptor.capture());
    JSONObject jsObj = new JSONObject(new String(bpmCaptor.getValue().m_payload, "UTF-8"));
    final int partitionId = jsObj.getInt(Cartographer.JSON_PARTITION_ID);
    final long initiatorHSId = jsObj.getLong(Cartographer.JSON_INITIATOR_HSID);
    assertEquals(MpInitiator.MP_INIT_PID, partitionId);
    assertEquals(3, initiatorHSId);
    mpwriter.shutdown();
}
Also used : BinaryPayloadMessage(org.voltcore.messaging.BinaryPayloadMessage) VoltMessage(org.voltcore.messaging.VoltMessage) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) JSONObject(org.json_voltpatches.JSONObject) HostMessenger(org.voltcore.messaging.HostMessenger) Matchers.anyLong(org.mockito.Matchers.anyLong) Test(org.junit.Test)

Example 34 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestCartographer method testRackAwareRejoin.

@Test
public void testRackAwareRejoin() throws Exception {
    ZooKeeper zk = getClient(0);
    VoltZK.createPersistentZKNodes(zk);
    HostMessenger hm = mock(HostMessenger.class);
    when(hm.getZK()).thenReturn(m_messengers.get(0).getZK());
    Cartographer dut = new Cartographer(hm, 0, false);
    // In total there are 4 partitions, let's say there are two nodes missing
    // and one is rejoining back
    int kfactor = 1;
    int sitesPerHost = 2;
    int hostCount = 4;
    int totalPartitions = (hostCount * sitesPerHost) / (kfactor + 1);
    Map<Integer, String> hostGroups = Maps.newHashMap();
    hostGroups.put(0, "rack1");
    // this is rejoining node
    hostGroups.put(1, "rack1");
    hostGroups.put(2, "rack2");
    // hostGroups.put(3, "rack2");  // this node is dead
    Set<Integer> deadHosts = Sets.newHashSet();
    deadHosts.add(1);
    deadHosts.add(3);
    int rejoiningHostId = 1;
    // Depends on number of partitions this part can be slow.
    int hostIdCounter = 0;
    int[] siteIds = new int[hostCount];
    for (int i = 0; i < hostCount; i++) {
        siteIds[i] = 0;
    }
    // assign partitions
    for (int pid = 0; pid < totalPartitions; pid++) {
        LeaderElector.createRootIfNotExist(zk, LeaderElector.electionDirForPartition(VoltZK.leaders_initiators, pid));
        // I'm evil
        Thread.sleep(500);
        assertFalse(VoltDB.wasCrashCalled);
        // add replica
        for (int k = 0; k <= kfactor; k++) {
            int hid = hostIdCounter++ % hostCount;
            if (deadHosts.contains(hid))
                continue;
            long HSId = CoreUtils.getHSIdFromHostAndSite(hid, siteIds[hid]++);
            LeaderElector.createParticipantNode(zk, LeaderElector.electionDirForPartition(VoltZK.leaders_initiators, pid), Long.toString(HSId++), null);
        }
    }
    /*
         * Partition layout should be:
         * H0: p0, p2
         * H1: ?, ?  (rejoining, expect p1, p3)
         * H2: p1, p3
         * H3: p0, p2 (dead)
         */
    List<Integer> expectedPartitions = Lists.newArrayList();
    expectedPartitions.add(1);
    expectedPartitions.add(3);
    List<Integer> partitionsToReplace = dut.getIv2PartitionsToReplace(kfactor, sitesPerHost, rejoiningHostId, hostGroups);
    assertTrue(partitionsToReplace.size() == sitesPerHost);
    for (Integer p : partitionsToReplace) {
        assertTrue(expectedPartitions.contains(p));
    }
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) HostMessenger(org.voltcore.messaging.HostMessenger) Test(org.junit.Test)

Example 35 with ZooKeeper

use of org.apache.zookeeper_voltpatches.ZooKeeper in project voltdb by VoltDB.

the class TestLeaderCache method testModifyChildWithCallback.

@Test
public void testModifyChildWithCallback() throws Exception {
    ZooKeeper zk = getClient(0);
    configure("/cache03", zk);
    TestCallback cb = new TestCallback();
    LeaderCache dut = new LeaderCache(zk, "/cache03", cb);
    dut.start(true);
    Map<Integer, Long> cache = cb.m_cache;
    assertEquals("3 items cached.", 3, cache.size());
    assertEquals(12345678, cache.get(0).longValue());
    dut.put(0, 23456789);
    while (true) {
        cache = cb.m_cache;
        if (cache.get(0) == 23456789) {
            break;
        }
    }
    cache = cb.m_cache;
    assertEquals("3 items cached.", 3, cache.size());
    assertEquals(23456789, cache.get(0).longValue());
    assertEquals(87654321, cache.get(1).longValue());
    assertEquals(11223344, cache.get(2).longValue());
    dut.shutdown();
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)62 Test (org.junit.Test)38 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)14 JSONObject (org.json_voltpatches.JSONObject)14 Semaphore (java.util.concurrent.Semaphore)11 Stat (org.apache.zookeeper_voltpatches.data.Stat)10 ExecutionException (java.util.concurrent.ExecutionException)8 HostMessenger (org.voltcore.messaging.HostMessenger)8 IOException (java.io.IOException)7 JSONException (org.json_voltpatches.JSONException)7 WatchedEvent (org.apache.zookeeper_voltpatches.WatchedEvent)5 Watcher (org.apache.zookeeper_voltpatches.Watcher)5 VoltTable (org.voltdb.VoltTable)5 SettingsException (org.voltdb.settings.SettingsException)5 List (java.util.List)4 Map (java.util.Map)4 HostAndPort (com.google_voltpatches.common.net.HostAndPort)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SocketException (java.net.SocketException)3 ImmutableMap (com.google_voltpatches.common.collect.ImmutableMap)2