@Test public void testCreate() throws IOException, KeeperException, InterruptedException { Stat stat = new Stat(); zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); Assert.assertEquals(0, stat.getEphemeralOwner()); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); fakeElapsed.set(1000); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
private void connect(TxZookeeperConfig config) { try { zooKeeper = new ZooKeeper(config.getHost(), config.getSessionTimeOut(), watchedEvent -> { if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) { // 放开闸门, wait在connect方法上的线程将被唤醒 COUNT_DOWN_LATCH.countDown(); } }); COUNT_DOWN_LATCH.await(); Stat stat = zooKeeper.exists(rootPath, false); if (stat == null) { zooKeeper.create(rootPath, rootPath.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (Exception e) { throw new TransactionIoException(e); } }
/** * 更改恢复次数 * * @param id 事务id * @param retry 恢复次数 * @param applicationName 应用名称 * @return true 成功 */ @Override public Boolean updateRetry(String id, Integer retry, String applicationName) { if (StringUtils.isBlank(id) || StringUtils.isBlank(applicationName) || Objects.isNull(retry)) { return Boolean.FALSE; } final String rootPath = RepositoryPathUtils.buildZookeeperPath(applicationName); final String path = buildRootPath(rootPath, id); try { byte[] content = zooKeeper.getData(path, false, new Stat()); final TransactionRecoverAdapter adapter = objectSerializer.deSerialize(content, TransactionRecoverAdapter.class); adapter.setLastTime(DateUtils.getDateYYYY()); adapter.setRetriedCount(retry); zooKeeper.create(path, objectSerializer.serialize(adapter), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return Boolean.TRUE; } catch (Exception e) { e.printStackTrace(); } return Boolean.FALSE; }
@Test public void testNodeChildrenChanged() throws Exception { QuorumUtil qu = new QuorumUtil(1); qu.startAll(); EventsWatcher watcher = new EventsWatcher(); ZooKeeper zk1 = createClient(qu, 1, watcher); ZooKeeper zk2 = createClient(qu, 2); String path = "/test-children-changed"; zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk1.getChildren(path, watcher); qu.shutdown(1); zk2.create(path + "/children-1", new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); qu.start(1); watcher.waitForConnected(TIMEOUT * 1000L); watcher.assertEvent(TIMEOUT, EventType.NodeChildrenChanged); qu.shutdownAll(); }
private int getItemPerms(TableItem item) { int perms = 0; Button allCheckBox = getItemPermCheckBox(item, ZooDefs.Perms.ALL); if (allCheckBox.getSelection()) { perms = ZooDefs.Perms.ALL; } else { for (int perm : PERMS) { Button permCheckBox = getItemPermCheckBox(item, perm); if (permCheckBox.getSelection()) { perms |= perm; } } } return perms; }
/** * @param zkServers * @param lockName * @param sessionTimeout */ public ZkDistributeLock(String zkServers, String lockName, int sessionTimeout) { if (lockName.contains(LOCK_KEY_SUFFIX)) { throw new LockException("lockName 不能包含[" + LOCK_KEY_SUFFIX + "]"); } this.lockName = lockName; this.sessionTimeout = sessionTimeout; try { zk = new ZooKeeper(zkServers, sessionTimeout, this); Stat stat = zk.exists(ROOT_PATH, false); if (stat == null) { // 创建根节点 zk.create(ROOT_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (Exception e) { throw new LockException(e); } }
private void sendSaslPacket(byte[] saslToken, ClientCnxn cnxn) throws SaslException{ if (LOG.isDebugEnabled()) { LOG.debug("ClientCnxn:sendSaslPacket:length="+saslToken.length); } GetSASLRequest request = new GetSASLRequest(); request.setToken(saslToken); SetSASLResponse response = new SetSASLResponse(); ServerSaslResponseCallback cb = new ServerSaslResponseCallback(); try { cnxn.sendPacket(request,response,cb, ZooDefs.OpCode.sasl); } catch (IOException e) { throw new SaslException("Failed to send SASL packet to server.", e); } }
private void addRequestToSyncProcessor() { long zxid = ZxidUtils.makeZxid(3, 7); TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.setData); Record txn = new SetDataTxn("/foo" + zxid, new byte[0], 1); byte[] buf; try { buf = Util.marshallTxnEntry(hdr, txn); } catch (IOException e) { LOG.error("IOException while adding request to SyncRequestProcessor", e); Assert.fail("IOException while adding request to SyncRequestProcessor!"); return; } NettyServerCnxnFactory factory = new NettyServerCnxnFactory(); final MockNettyServerCnxn nettyCnxn = new MockNettyServerCnxn(null, this, factory); Request req = new Request(nettyCnxn, 1, 1, ZooDefs.OpCode.setData, ByteBuffer.wrap(buf), null); req.hdr = hdr; req.txn = txn; syncProcessor.processRequest(req); }
@Test public void testNodeCreated() throws Exception { QuorumUtil qu = new QuorumUtil(1); qu.startAll(); EventsWatcher watcher = new EventsWatcher(); ZooKeeper zk1 = createClient(qu, 1, watcher); ZooKeeper zk2 = createClient(qu, 2); String path = "/test1-created"; zk1.exists(path, watcher); qu.shutdown(1); zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); qu.start(1); watcher.waitForConnected(TIMEOUT * 1000L); watcher.assertEvent(TIMEOUT, EventType.NodeCreated); qu.shutdownAll(); }
private static void resetAcls(final ZooKeeperWatcher zkw, final String znode, final boolean eraseAcls) throws Exception { List<String> children = ZKUtil.listChildrenNoWatch(zkw, znode); if (children != null) { for (String child: children) { resetAcls(zkw, ZKUtil.joinZNode(znode, child), eraseAcls); } } ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper(); if (eraseAcls) { LOG.info(" - erase ACLs for " + znode); zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1); } else { LOG.info(" - set ACLs for " + znode); zk.setACL(znode, ZKUtil.createACL(zkw, znode, true), -1); } }
/** * Tests a situation when client firstly connects to a read-only server and * then connects to a majority server. Transition should be transparent for * the user. */ @Test(timeout = 90000) public void testSessionEstablishment() throws Exception { qu.shutdown(2); CountdownWatcher watcher = new CountdownWatcher(); ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); watcher.waitForConnected(CONNECTION_TIMEOUT); Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk .getState()); long fakeId = zk.getSessionId(); watcher.reset(); qu.start(2); Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp( "127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); watcher.waitForConnected(CONNECTION_TIMEOUT); zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertFalse("fake session and real session have same id", zk .getSessionId() == fakeId); zk.close(); }
/** * 更新 List<Participant> 只更新这一个字段数据 * * @param tccTransaction 实体对象 */ @Override public int updateParticipant(TccTransaction tccTransaction) { final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix, tccTransaction.getTransId()); try { byte[] content = zooKeeper.getData(path, false, new Stat()); final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class); adapter.setContents(objectSerializer.serialize(tccTransaction.getParticipants())); zooKeeper.create(path, objectSerializer.serialize(adapter), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return 1; } catch (Exception e) { e.printStackTrace(); return 0; } }
/** * 更新补偿数据状态 * * @param id 事务id * @param status 状态 * @return rows 1 成功 0 失败 */ @Override public int updateStatus(String id, Integer status) { final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix,id); try { byte[] content = zooKeeper.getData(path, false, new Stat()); final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class); adapter.setStatus(status); zooKeeper.create(path, objectSerializer.serialize(adapter), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return 1; } catch (Exception e) { e.printStackTrace(); return 0; } }
private void connect(TccZookeeperConfig config) { try { zooKeeper = new ZooKeeper(config.getHost(), config.getSessionTimeOut(), watchedEvent -> { if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) { // 放开闸门, wait在connect方法上的线程将被唤醒 LATCH.countDown(); } }); LATCH.await(); Stat stat = zooKeeper.exists(rootPathPrefix, false); if (stat == null) { zooKeeper.create(rootPathPrefix, rootPathPrefix.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (Exception e) { throw new TccRuntimeException(e); } }
/** * 更改恢复次数 * * @param id 事务id * @param retry 恢复次数 * @param applicationName 应用名称 * @return true 成功 */ @Override public Boolean updateRetry(String id, Integer retry, String applicationName) { if (StringUtils.isBlank(id) || StringUtils.isBlank(applicationName) || Objects.isNull(retry)) { return Boolean.FALSE; } final String rootPathPrefix = RepositoryPathUtils.buildZookeeperPathPrefix(applicationName); final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix, id); try { byte[] content = zooKeeper.getData(path, false, new Stat()); final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class); adapter.setLastTime(DateUtils.getDateYYYY()); adapter.setRetriedCount(retry); zooKeeper.create(path, objectSerializer.serialize(adapter), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return Boolean.TRUE; } catch (Exception e) { e.printStackTrace(); } return Boolean.FALSE; }
/** * @param path * @return */ private static Stat createWithParent(String path){ if (null == path || path.trim().length()==0) { return null; } try { Stat stat = getInstance().exists(path, true); if(null == stat) { if(path.lastIndexOf("/") > 0) { String parentPath = path.substring(0,path.lastIndexOf("/")); Stat parentStat = getInstance().exists(parentPath, true); if (null == parentStat) { createWithParent(parentPath); } } //添加节点 zooKeeper.create(path, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } return getInstance().exists(path, true); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 更新 List<Participant> 只更新这一个字段数据 * * @param mythTransaction 实体对象 */ @Override public int updateParticipant(MythTransaction mythTransaction) throws MythRuntimeException { final String path = RepositoryPathUtils .buildZookeeperRootPath(rootPathPrefix, mythTransaction.getTransId()); try { byte[] content = zooKeeper.getData(path, false, new Stat()); final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class); adapter.setContents(objectSerializer.serialize(mythTransaction.getMythParticipants())); zooKeeper.create(path, objectSerializer.serialize(adapter), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return CommonConstant.SUCCESS; } catch (Exception e) { throw new MythRuntimeException(e); } }
static private int getPermFromString(String permString) { int perm = 0; for (int i = 0; i < permString.length(); i++) { switch (permString.charAt(i)) { case 'r': perm |= ZooDefs.Perms.READ; break; case 'w': perm |= ZooDefs.Perms.WRITE; break; case 'c': perm |= ZooDefs.Perms.CREATE; break; case 'd': perm |= ZooDefs.Perms.DELETE; break; case 'a': perm |= ZooDefs.Perms.ADMIN; break; default: System.err.println("Unknown perm type: " + permString.charAt(i)); } } return perm; }
private void createZookeeper(final CountDownLatch connectionLatch) throws Exception { zk = new ZooKeeper(this.properties.getProperty(keys.zkConnectString .toString()), Integer.parseInt(this.properties .getProperty(keys.zkSessionTimeout.toString())), new Watcher() { public void process(WatchedEvent event) { sessionEvent(connectionLatch, event); } }); String authString = this.properties.getProperty(keys.userName.toString()) + ":"+ this.properties.getProperty(keys.password.toString()); this.isCheckParentPath = Boolean.parseBoolean(this.properties.getProperty(keys.isCheckParentPath.toString(),"true")); zk.addAuthInfo("digest", authString.getBytes()); acl.clear(); acl.add(new ACL(ZooDefs.Perms.ALL, new Id("digest", DigestAuthenticationProvider.generateDigest(authString)))); acl.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE)); }
static boolean grabTicket(ZooKeeper zookeeper, String lockNode, String ticket) throws InterruptedException, KeeperException { try { zookeeper.create(lockNode + "/" + ticket, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } catch (KeeperException e) { if (e.code() == KeeperException.Code.NODEEXISTS) { logger.debug("Failed to claim ticket {}.", ticket); return false; } else { throw e; } } logger.debug("Claimed ticket {}.", ticket); return true; }
@Test public void testModifying() throws IOException, KeeperException, InterruptedException { Stat stat = new Stat(); zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); Assert.assertEquals(0, stat.getEphemeralOwner()); final AtomicLong fakeElapsed = new AtomicLong(0); ContainerManager containerManager = newContainerManager(fakeElapsed); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); for ( int i = 0; i < 10; ++i ) { fakeElapsed.set(50); zk.setData("/foo", new byte[i + 1], -1); containerManager.checkContainers(); Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); } fakeElapsed.set(200); containerManager.checkContainers(); Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); }
@Test public void directCheckUpgradeSessionTest() throws IOException, InterruptedException, KeeperException { final ZooKeeper zk = createClient(); String path = "/directcheckupgradesession"; zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId()); Assert.assertNotNull("unable to find server interlocutor", server); Request readRequest = makeGetDataRequest(path, zk.getSessionId()); Request createRequest = makeCreateRequest(path + "/e", zk.getSessionId()); Assert.assertNull("tried to upgrade on a read", server.checkUpgradeSession(readRequest)); Assert.assertNotNull("failed to upgrade on a create", server.checkUpgradeSession(createRequest)); Assert.assertNull("tried to upgrade after successful promotion", server.checkUpgradeSession(createRequest)); }
@Test public void testGetTxnLogSyncElapsedTime() throws IOException { File tmpDir = ClientBase.createEmptyTestDir(); FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog")); TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData); Record txn = new SetDataTxn("/foo", new byte[0], 1); Request req = new Request(0, 0, 0, hdr, txn, 0); try { fileTxnSnapLog.append(req); fileTxnSnapLog.commit(); long syncElapsedTime = fileTxnSnapLog.getTxnLogElapsedSyncTime(); Assert.assertNotEquals("Did not update syncElapsedTime!", -1L, syncElapsedTime); } finally { fileTxnSnapLog.close(); } }
/** * converts a long to a list of acls. * * @param longVal * @return a list of ACLs that map to the long */ public synchronized List<ACL> convertLong(Long longVal) { if (longVal == null) return null; if (longVal == OPEN_UNSAFE_ACL_ID) return ZooDefs.Ids.OPEN_ACL_UNSAFE; List<ACL> acls = longKeyMap.get(longVal); if (acls == null) { LOG.error("ERROR: ACL not available for long " + longVal); throw new RuntimeException("Failed to fetch acls for " + longVal); } return acls; }
@Before public void setUp() throws Exception { super.setUp(); zooKeeper = createClient(); zooKeeper.create(testRootNode + Thread.currentThread().getId(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }
@Test public void testCreateNode1() throws Exception { ACL aclIp = new ACL(ZooDefs.Perms.READ,new Id("ip","192.168.1.105")); ACL aclDigest = new ACL(ZooDefs.Perms.READ| ZooDefs.Perms.WRITE,new Id("digest", ZKBase64AuthUtils.getDigest("aric","123456"))); ArrayList<ACL> acls = new ArrayList<ACL>(); acls.add(aclIp);acls.add(aclDigest); String path= zkSyncService.createNode("/node_4", "123".getBytes(),acls, CreateMode.PERSISTENT); PrinterUtils.printELog(path + "" + ZKWatcher.getWatchedEvent()); }
private void append(ZKDatabase zkdb, int i) throws IOException { TxnHeader hdr = new TxnHeader(1, 1, i, 1, ZooDefs.OpCode.setData); Record txn = new SetDataTxn("/foo" + i, new byte[0], 1); Request req = new Request(null, 0, 0, 0, null, null); req.hdr = hdr; req.txn = txn; zkdb.append(req); zkdb.commit(); }
private ACLProvider aclProvider() { return new ACLProvider() { @Override public List<ACL> getDefaultAcl() { return ZooDefs.Ids.CREATOR_ALL_ACL; } @Override public List<ACL> getAclForPath(String path) { return ZooDefs.Ids.CREATOR_ALL_ACL; } }; }
public ACLProvider aclProvider() { return new ACLProvider() { @Override public List<ACL> getDefaultAcl() { return ZooDefs.Ids.CREATOR_ALL_ACL; } @Override public List<ACL> getAclForPath(String path) { return ZooDefs.Ids.CREATOR_ALL_ACL; } }; }
@Bean public ACLProvider aclProvider() { return new ACLProvider() { @Override public List<ACL> getDefaultAcl() { return ZooDefs.Ids.CREATOR_ALL_ACL; } @Override public List<ACL> getAclForPath(String path) { return ZooDefs.Ids.CREATOR_ALL_ACL; } }; }
/** * createNodeWithACL * Create a node under ACL mode * @param path * @param payload * @throws Exception */ public void createNodeWithACL(String path, byte[] payload) throws Exception { ACL acl = new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS); List<ACL> aclList = Lists.newArrayList(acl); try { client.create().withACL(aclList).forPath(path, payload); } catch (Exception e) { logger.error("Create security file failed."); e.printStackTrace(); } }
/** * When authentication is enabled on Zookeeper, /hbase/master should be * created with 2 ACLs: one specifies that the hbase user has full access * to the node; the other, that it is world-readable. */ @Test (timeout=30000) public void testHBaseMasterServerZNodeACL() throws Exception { if (!secureZKAvailable) { return; } List<ACL> acls = zkw.getRecoverableZooKeeper().getZooKeeper() .getACL("/hbase/master", new Stat()); assertEquals(acls.size(),2); boolean foundWorldReadableAcl = false; boolean foundHBaseOwnerAcl = false; for(int i = 0; i < 2; i++) { if (acls.get(i).getId().getScheme().equals("world") == true) { assertEquals(acls.get(0).getId().getId(),"anyone"); assertEquals(acls.get(0).getPerms(), ZooDefs.Perms.READ); foundWorldReadableAcl = true; } else { if (acls.get(i).getId().getScheme().equals("sasl") == true) { assertEquals(acls.get(1).getId().getId(),"hbase"); assertEquals(acls.get(1).getId().getScheme(),"sasl"); foundHBaseOwnerAcl = true; } else { // error: should not get here: test fails. assertTrue(false); } } } assertTrue(foundWorldReadableAcl); assertTrue(foundHBaseOwnerAcl); }
private void addNewItem() { ACL acl = new ACL(); acl.setId(ZooDefs.Ids.ANYONE_ID_UNSAFE); acl.setPerms(ZooDefs.Perms.ALL); TableItem newItem = addAclTableItem(acl); Table table = getTable(); table.setSelection(newItem); tableSelectionChanged(newItem); fireOrchestrationChange(); }
private void makeOffer() throws KeeperException, InterruptedException { state = State.OFFER; dispatchEvent(EventType.OFFER_START); leaderOffer = new LeaderOffer(); leaderOffer.setHostName(hostName); leaderOffer.setNodePath(zooKeeper.create(rootNodeName + "/" + "n_", hostName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL)); logger.debug("Created leader offer {}", leaderOffer); dispatchEvent(EventType.OFFER_COMPLETE); }