@Test public void testKeyUpdate() throws Exception { assertTrue(KEY_MASTER.isMaster()); assertFalse(KEY_SLAVE.isMaster()); int maxKeyId = 0; KEY_MASTER.rollCurrentKey(); AuthenticationKey key1 = KEY_MASTER.getCurrentKey(); assertNotNull(key1); LOG.debug("Master current key: "+key1.getKeyId()); Thread.sleep(1000); AuthenticationKey slaveCurrent = KEY_SLAVE.getCurrentKey(); assertNotNull(slaveCurrent); assertEquals(key1, slaveCurrent); LOG.debug("Slave current key: "+slaveCurrent.getKeyId()); KEY_MASTER.rollCurrentKey(); AuthenticationKey key2 = KEY_MASTER.getCurrentKey(); LOG.debug("Master new current key: "+key2.getKeyId()); KEY_MASTER.rollCurrentKey(); AuthenticationKey key3 = KEY_MASTER.getCurrentKey(); LOG.debug("Master new current key: "+key3.getKeyId()); key1.setExpiration(EnvironmentEdgeManager.currentTimeMillis() - 1000); KEY_MASTER.removeExpiredKeys(); assertNull(KEY_MASTER.getKey(key1.getKeyId())); KEY_SLAVE.getLatch().await(); AuthenticationKey slave2 = KEY_SLAVE.getKey(key2.getKeyId()); assertNotNull(slave2); assertEquals(key2, slave2); AuthenticationKey slave3 = KEY_SLAVE.getKey(key3.getKeyId()); assertNotNull(slave3); assertEquals(key3, slave3); slaveCurrent = KEY_SLAVE.getCurrentKey(); assertEquals(key3, slaveCurrent); LOG.debug("Slave current key: "+slaveCurrent.getKeyId()); assertNull(KEY_SLAVE.getKey(key1.getKeyId())); Configuration conf = TEST_UTIL.getConfiguration(); ZooKeeperWatcher zk = newZK(conf, "server3", new MockAbortable()); KEY_SLAVE2 = new AuthenticationTokenSecretManager( conf, zk, "server3", 60*60*1000, 60*1000); KEY_SLAVE2.start(); Thread.sleep(1000); slave2 = KEY_SLAVE2.getKey(key2.getKeyId()); assertNotNull(slave2); assertEquals(key2, slave2); slave3 = KEY_SLAVE2.getKey(key3.getKeyId()); assertNotNull(slave3); assertEquals(key3, slave3); slaveCurrent = KEY_SLAVE2.getCurrentKey(); assertEquals(key3, slaveCurrent); assertNull(KEY_SLAVE2.getKey(key1.getKeyId())); KEY_MASTER.stop(); Thread.sleep(1000); assertFalse(KEY_MASTER.isMaster()); AuthenticationTokenSecretManager[] mgrs = new AuthenticationTokenSecretManager[]{ KEY_SLAVE, KEY_SLAVE2 }; AuthenticationTokenSecretManager newMaster = null; int tries = 0; while (newMaster == null && tries++ < 5) { for (AuthenticationTokenSecretManager mgr : mgrs) { if (mgr.isMaster()) { newMaster = mgr; break; } } if (newMaster == null) { Thread.sleep(500); } } assertNotNull(newMaster); AuthenticationKey current = newMaster.getCurrentKey(); assertTrue(current.getKeyId() >= slaveCurrent.getKeyId()); LOG.debug("New master, current key: "+current.getKeyId()); newMaster.rollCurrentKey(); AuthenticationKey newCurrent = newMaster.getCurrentKey(); LOG.debug("New master, rolled new current key: "+newCurrent.getKeyId()); assertTrue(newCurrent.getKeyId() > current.getKeyId()); ZooKeeperWatcher zk3 = newZK(conf, "server4", new MockAbortable()); KEY_SLAVE3 = new AuthenticationTokenSecretManager( conf, zk3, "server4", 60*60*1000, 60*1000); KEY_SLAVE3.start(); Thread.sleep(5000); newMaster.stop(); Thread.sleep(5000); assertFalse(newMaster.isMaster()); mgrs = new AuthenticationTokenSecretManager[]{ KEY_SLAVE, KEY_SLAVE2, KEY_SLAVE3 }; newMaster = null; tries = 0; while (newMaster == null && tries++ < 5) { for (AuthenticationTokenSecretManager mgr : mgrs) { if (mgr.isMaster()) { newMaster = mgr; break; } } if (newMaster == null) { Thread.sleep(500); } } assertNotNull(newMaster); AuthenticationKey current2 = newMaster.getCurrentKey(); assertTrue(current2.getKeyId() >= newCurrent.getKeyId()); LOG.debug("New master 2, current key: "+current2.getKeyId()); newMaster.rollCurrentKey(); AuthenticationKey newCurrent2 = newMaster.getCurrentKey(); LOG.debug("New master 2, rolled new current key: "+newCurrent2.getKeyId()); assertTrue(newCurrent2.getKeyId() > current2.getKeyId()); }
public void start() throws KeeperException { watcher.registerListener(this); ZKUtil.createWithParents(watcher, keysParentZNode); if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) { List<ZKUtil.NodeAndData> nodes = ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode); refreshNodes(nodes); } }
ZKSecretWatcher extends ZooKeeperListener { public void start() throws KeeperException { watcher.registerListener(this); ZKUtil.createWithParents(watcher, keysParentZNode); if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) { List<ZKUtil.NodeAndData> nodes = ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode); refreshNodes(nodes); } } }
ZKSecretWatcher extends ZooKeeperListener { public void start() throws KeeperException { watcher.registerListener(this); ZKUtil.createWithParents(watcher, keysParentZNode); if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) { List<ZKUtil.NodeAndData> nodes = ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode); refreshNodes(nodes); } } ZKSecretWatcher(Configuration conf,
      ZooKeeperWatcher watcher,
      AuthenticationTokenSecretManager secretManager); }
ZKSecretWatcher extends ZooKeeperListener { public void start() throws KeeperException { watcher.registerListener(this); ZKUtil.createWithParents(watcher, keysParentZNode); if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) { List<ZKUtil.NodeAndData> nodes = ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode); refreshNodes(nodes); } } ZKSecretWatcher(Configuration conf,
      ZooKeeperWatcher watcher,
      AuthenticationTokenSecretManager secretManager); void start(); @Override void nodeCreated(String path); @Override void nodeDeleted(String path); @Override void nodeDataChanged(String path); @Override void nodeChildrenChanged(String path); String getRootKeyZNode(); void removeKeyFromZK(AuthenticationKey key); void addKeyToZK(AuthenticationKey key); void updateKeyInZK(AuthenticationKey key); }
ZKSecretWatcher extends ZooKeeperListener { public void start() throws KeeperException { watcher.registerListener(this); ZKUtil.createWithParents(watcher, keysParentZNode); if (ZKUtil.watchAndCheckExists(watcher, keysParentZNode)) { List<ZKUtil.NodeAndData> nodes = ZKUtil.getChildDataAndWatchForNewChildren(watcher, keysParentZNode); refreshNodes(nodes); } } ZKSecretWatcher(Configuration conf,
      ZooKeeperWatcher watcher,
      AuthenticationTokenSecretManager secretManager); void start(); @Override void nodeCreated(String path); @Override void nodeDeleted(String path); @Override void nodeDataChanged(String path); @Override void nodeChildrenChanged(String path); String getRootKeyZNode(); void removeKeyFromZK(AuthenticationKey key); void addKeyToZK(AuthenticationKey key); void updateKeyInZK(AuthenticationKey key);  }