use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class PurgeTxnTest method testPurgeWhenLogRollingInProgress.
/**
* Tests purge when logs are rolling or a new snapshot is created, then
* these newer files should alse be excluded in the current cycle.
*
* For frequent snapshotting, configured SnapCount to 30. There are three
* threads which will create 1000 znodes each and simultaneously do purge
* call
*/
@Test
public void testPurgeWhenLogRollingInProgress() throws Exception {
tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(30);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
final ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
final CountDownLatch doPurge = new CountDownLatch(1);
final CountDownLatch purgeFinished = new CountDownLatch(1);
final AtomicBoolean opFailed = new AtomicBoolean(false);
new Thread() {
public void run() {
try {
doPurge.await(OP_TIMEOUT_IN_MILLIS / 2, TimeUnit.MILLISECONDS);
PurgeTxnLog.purge(tmpDir, tmpDir, 3);
} catch (IOException ioe) {
LOG.error("Exception when purge", ioe);
opFailed.set(true);
} catch (InterruptedException ie) {
LOG.error("Exception when purge", ie);
opFailed.set(true);
} finally {
purgeFinished.countDown();
}
}
;
}.start();
final int thCount = 3;
List<String> znodes = manyClientOps(zk, doPurge, thCount, "/invalidsnap");
Assert.assertTrue("Purging is not finished!", purgeFinished.await(OP_TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS));
Assert.assertFalse("Purging failed!", opFailed.get());
for (String znode : znodes) {
try {
zk.getData(znode, false, null);
} catch (Exception ke) {
LOG.error("Unexpected exception when visiting znode!", ke);
Assert.fail("Unexpected exception when visiting znode!");
}
}
zk.close();
f.shutdown();
zks.shutdown();
zks.getTxnLogFactory().close();
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class PurgeTxnTest method testPurgeDoesNotDeleteOverlappingLogFile.
/**
* Verifies that purge does not delete any log files which started before the oldest retained
* snapshot but which might extend beyond it.
* @throws Exception an exception might be thrown here
*/
@Test
public void testPurgeDoesNotDeleteOverlappingLogFile() throws Exception {
// Setting used for snapRetainCount in this test.
final int SNAP_RETAIN_COUNT = 3;
// Number of znodes this test creates in each snapshot.
final int NUM_ZNODES_PER_SNAPSHOT = 100;
/**
* Set a sufficiently high snapCount to ensure that we don't rollover the log. Normally,
* the default value (100K at time of this writing) would ensure this, but we make that
* dependence explicit here to make the test future-proof. Not rolling over the log is
* important for this test since we are testing retention of the one and only log file which
* predates each retained snapshot.
*/
SyncRequestProcessor.setSnapCount(SNAP_RETAIN_COUNT * NUM_ZNODES_PER_SNAPSHOT * 10);
// Create Zookeeper and connect to it.
tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
// Unique identifier for each znode that we create.
int unique = 0;
try {
/**
* Create some znodes and take a snapshot. Repeat this until we have SNAP_RETAIN_COUNT
* snapshots. Do not rollover the log.
*/
for (int snapshotCount = 0; snapshotCount < SNAP_RETAIN_COUNT; snapshotCount++) {
for (int i = 0; i < 100; i++, unique++) {
zk.create("/snap-" + unique, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zks.takeSnapshot();
}
// Create some additional znodes without taking a snapshot afterwards.
for (int i = 0; i < 100; i++, unique++) {
zk.create("/snap-" + unique, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} finally {
zk.close();
}
// Shutdown Zookeeper.
f.shutdown();
zks.getTxnLogFactory().close();
zks.shutdown();
Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
// Purge snapshot and log files.
PurgeTxnLog.purge(tmpDir, tmpDir, SNAP_RETAIN_COUNT);
// Initialize Zookeeper again from the same dataDir.
zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
zk = ClientBase.createZKClient(HOSTPORT);
/**
* Verify that the last znode that was created above exists. This znode's creation was
* captured by the transaction log which was created before any of the above
* SNAP_RETAIN_COUNT snapshots were created, but it's not captured in any of these
* snapshots. So for it it exist, the (only) existing log file should not have been purged.
*/
final String lastZnode = "/snap-" + (unique - 1);
final Stat stat = zk.exists(lastZnode, false);
Assert.assertNotNull("Last znode does not exist: " + lastZnode, stat);
// Shutdown for the last time.
f.shutdown();
zks.getTxnLogFactory().close();
zks.shutdown();
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class PurgeTxnTest method testPurge.
/**
* test the purge
* @throws Exception an exception might be thrown here
*/
@Test
public void testPurge() throws Exception {
tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(100);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
try {
for (int i = 0; i < 2000; i++) {
zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
} finally {
zk.close();
}
f.shutdown();
zks.getTxnLogFactory().close();
Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
// now corrupt the snapshot
PurgeTxnLog.purge(tmpDir, tmpDir, 3);
FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir);
List<File> listLogs = snaplog.findNRecentSnapshots(4);
int numSnaps = 0;
for (File ff : listLogs) {
if (ff.getName().startsWith("snapshot")) {
numSnaps++;
}
}
Assert.assertTrue("exactly 3 snapshots ", (numSnaps == 3));
snaplog.close();
zks.shutdown();
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class ACLCountTest method testAclCount.
/**
*
* Create a node and add 4 ACL values to it, but there are only 2 unique ACL values,
* and each is repeated once:
*
* ACL(ZooDefs.Perms.READ,ZooDefs.Ids.ANYONE_ID_UNSAFE);
* ACL(ZooDefs.Perms.ALL,ZooDefs.Ids.AUTH_IDS);
* ACL(ZooDefs.Perms.READ,ZooDefs.Ids.ANYONE_ID_UNSAFE);
* ACL(ZooDefs.Perms.ALL,ZooDefs.Ids.AUTH_IDS);
*
* Even though we've added 4 ACL values, there should only be 2 ACLs for that node,
* since there are only 2 *unique* ACL values.
*/
@Test
public void testAclCount() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(1000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
ZooKeeper zk;
final ArrayList<ACL> CREATOR_ALL_AND_WORLD_READABLE = new ArrayList<ACL>() {
{
add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS));
add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS));
}
};
try {
LOG.info("starting up the zookeeper server .. waiting");
Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
zk = ClientBase.createZKClient(HOSTPORT);
zk.addAuthInfo("digest", "pat:test".getBytes());
zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
String path = "/path";
try {
Assert.assertEquals(4, CREATOR_ALL_AND_WORLD_READABLE.size());
} catch (Exception e) {
LOG.error("Something is fundamentally wrong with ArrayList's add() method. add()ing four times to an empty ArrayList should result in an ArrayList with 4 members.");
throw e;
}
zk.create(path, path.getBytes(), CREATOR_ALL_AND_WORLD_READABLE, CreateMode.PERSISTENT);
List<ACL> acls = zk.getACL("/path", new Stat());
Assert.assertEquals(2, acls.size());
} catch (Exception e) {
// test failed somehow.
Assert.assertTrue(false);
}
f.shutdown();
zks.shutdown();
}
use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.
the class ACLTest method testDisconnectedAddAuth.
@Test
public void testDisconnectedAddAuth() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(1000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
try {
LOG.info("starting up the zookeeper server .. waiting");
Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
try {
zk.addAuthInfo("digest", "pat:test".getBytes());
zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
} finally {
zk.close();
}
} finally {
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
}
}
Aggregations