@Test public void testTimestampUtilGetOldestTs() throws Exception { Assert.assertEquals(0, getOldestTs()); TimestampTracker tr1 = new TimestampTracker(env, new TransactorID(env), 5); final long ts1 = tr1.allocateTimestamp().getTxTimestamp(); Thread.sleep(15); Assert.assertEquals(tr1.getZookeeperTimestamp(), getOldestTs()); TimestampTracker tr2 = new TimestampTracker(env, new TransactorID(env), 5); final long ts2 = tr2.allocateTimestamp().getTxTimestamp(); TimestampTracker tr3 = new TimestampTracker(env, new TransactorID(env), 5); final long ts3 = tr3.allocateTimestamp().getTxTimestamp(); Thread.sleep(15); Assert.assertEquals(ts1, getOldestTs()); tr1.removeTimestamp(ts1); Thread.sleep(15); Assert.assertEquals(ts2, getOldestTs()); tr2.removeTimestamp(ts2); Thread.sleep(15); Assert.assertEquals(ts3, getOldestTs()); tr3.removeTimestamp(ts3); tr1.close(); tr2.close(); tr3.close(); }
private boolean zkNodeExists(TimestampTracker tracker) throws Exception { return env.getSharedResources().getCurator().checkExists() .forPath(tracker.getNodePath()) != null; }
@VisibleForTesting public synchronized void updateZkNode() { Preconditions.checkState(!updatingZk, "unexpected concurrent ZK update"); if (allocationsInProgress > 0) { if (!timestamps.isEmpty()) { updateZkNode(timestamps.first()); } } else if (allocationsInProgress == 0) { closeZkNode(); } else { throw new IllegalStateException("allocationsInProgress = " + allocationsInProgress); } }
/** * Allocate a timestamp */ public Stamp allocateTimestamp() { synchronized (this) { Preconditions.checkState(!closed, "tracker closed "); if (node == null) { Preconditions.checkState(allocationsInProgress == 0, "expected allocationsInProgress == 0 when node == null"); Preconditions.checkState(!updatingZk, "unexpected concurrent ZK update"); createZkNode(getTimestamp().getTxTimestamp()); } allocationsInProgress++; } try { Stamp ts = getTimestamp(); synchronized (this) { timestamps.add(ts.getTxTimestamp()); } return ts; } catch (RuntimeException re) { synchronized (this) { allocationsInProgress--; } throw re; } }
@Test public void testTrackingWithNoUpdate() throws Exception { TimestampTracker tracker = new TimestampTracker(env, new TransactorID(env)); Assert.assertTrue(tracker.isEmpty()); Assert.assertFalse(zkNodeExists(tracker)); final long ts1 = tracker.allocateTimestamp().getTxTimestamp(); Assert.assertFalse(tracker.isEmpty()); Assert.assertTrue(zkNodeExists(tracker)); Assert.assertTrue(ts1 > zkNodeValue(tracker)); Assert.assertEquals(tracker.getZookeeperTimestamp(), zkNodeValue(tracker)); Assert.assertEquals(ts1, tracker.getOldestActiveTimestamp()); final long ts2 = tracker.allocateTimestamp().getTxTimestamp(); Assert.assertEquals(ts1, tracker.getOldestActiveTimestamp()); tracker.removeTimestamp(ts1); Assert.assertFalse(tracker.isEmpty()); Assert.assertEquals(ts2, tracker.getOldestActiveTimestamp()); Assert.assertFalse(tracker.isEmpty()); Assert.assertTrue(ts1 > zkNodeValue(tracker)); Assert.assertEquals(tracker.getZookeeperTimestamp(), zkNodeValue(tracker)); tracker.removeTimestamp(ts2); Assert.assertTrue(tracker.isEmpty()); Assert.assertTrue(zkNodeExists(tracker)); tracker.close(); }
private static Stamp allocateTimestamp(Environment env) { return env.getSharedResources().getTimestampTracker().allocateTimestamp(); }
private synchronized void close(boolean checkForStaleScan) { if (status != TxStatus.CLOSED) { status = TxStatus.CLOSED; if (checkForStaleScan && !commitAttempted) { Stamp stamp = env.getSharedResources().getOracleClient().getStamp(); if (startTs < stamp.getGcTimestamp()) { throw new StaleScanException(); } } env.getSharedResources().getTimestampTracker().removeTimestamp(startTs); } }
public synchronized TimestampTracker getTimestampTracker() { checkIfClosed(); if (tsTracker == null) { tsTracker = new TimestampTracker(env, getTransactorID()); } return tsTracker; }
tsTracker.close();
@Test(expected = NoSuchElementException.class) public void testTsNoElement() { TimestampTracker tracker = env.getSharedResources().getTimestampTracker(); Assert.assertTrue(tracker.isEmpty()); tracker.getOldestActiveTimestamp(); }
private void waitForGcTime(long expectedTime) throws Exception { env.getSharedResources().getTimestampTracker().updateZkNode(); long oldestTs = ZookeeperUtil.getGcTimestamp(config.getAppZookeepers()); while (oldestTs < expectedTime) { Thread.sleep(500); oldestTs = ZookeeperUtil.getGcTimestamp(config.getAppZookeepers()); } }
@Override public synchronized void close() { Preconditions.checkState(!closed, "tracker already closed"); closed = true; timer.cancel(); closeZkNode(); } }
@Test public void testTrackingWithZkUpdate() throws Exception { TimestampTracker tracker = new TimestampTracker(env, new TransactorID(env), 5); final long ts1 = tracker.allocateTimestamp().getTxTimestamp(); Thread.sleep(15); Assert.assertNotNull(ts1); Assert.assertTrue(zkNodeExists(tracker)); Assert.assertNotNull(zkNodeValue(tracker)); Assert.assertEquals(tracker.getZookeeperTimestamp(), zkNodeValue(tracker)); Assert.assertEquals(ts1, tracker.getOldestActiveTimestamp()); final long ts2 = tracker.allocateTimestamp().getTxTimestamp(); Assert.assertEquals(ts1, tracker.getOldestActiveTimestamp()); Thread.sleep(15); tracker.removeTimestamp(ts1); Thread.sleep(15); Assert.assertEquals(ts2, tracker.getOldestActiveTimestamp()); Assert.assertEquals(ts2, zkNodeValue(tracker)); tracker.removeTimestamp(ts2); Thread.sleep(15); Assert.assertTrue(tracker.isEmpty()); Assert.assertFalse(zkNodeExists(tracker)); tracker.close(); }
private static Stamp allocateTimestamp(Environment env) { return env.getSharedResources().getTimestampTracker().allocateTimestamp(); }
private synchronized void close(boolean checkForStaleScan) { if (status != TxStatus.CLOSED) { status = TxStatus.CLOSED; if (checkForStaleScan && !commitAttempted) { Stamp stamp = env.getSharedResources().getOracleClient().getStamp(); if (startTs < stamp.getGcTimestamp()) { throw new StaleScanException(); } } env.getSharedResources().getTimestampTracker().removeTimestamp(startTs); } }
public synchronized TimestampTracker getTimestampTracker() { checkIfClosed(); if (tsTracker == null) { tsTracker = new TimestampTracker(env, getTransactorID()); } return tsTracker; }
/** * Allocate a timestamp */ public Stamp allocateTimestamp() { synchronized (this) { Preconditions.checkState(!closed, "tracker closed "); if (node == null) { Preconditions.checkState(allocationsInProgress == 0, "expected allocationsInProgress == 0 when node == null"); Preconditions.checkState(!updatingZk, "unexpected concurrent ZK update"); createZkNode(getTimestamp().getTxTimestamp()); } allocationsInProgress++; } try { Stamp ts = getTimestamp(); synchronized (this) { timestamps.add(ts.getTxTimestamp()); } return ts; } catch (RuntimeException re) { synchronized (this) { allocationsInProgress--; } throw re; } }
tsTracker.close();
@Override public synchronized void close() { Preconditions.checkState(!closed, "tracker already closed"); closed = true; timer.cancel(); closeZkNode(); } }
/** * Configure properties needed to connect to a Fluo application * * @param conf Job configuration * @param config use {@link FluoConfiguration} to configure programmatically */ public static void configure(Job conf, SimpleConfiguration config) { try { FluoConfiguration fconfig = new FluoConfiguration(config); try (Environment env = new Environment(fconfig)) { long ts = env.getSharedResources().getTimestampTracker().allocateTimestamp().getTxTimestamp(); conf.getConfiguration().setLong(TIMESTAMP_CONF_KEY, ts); ByteArrayOutputStream baos = new ByteArrayOutputStream(); config.save(baos); conf.getConfiguration().set(PROPS_CONF_KEY, new String(baos.toByteArray(), StandardCharsets.UTF_8)); AccumuloInputFormat.setClientInfo(conf, AccumuloUtil.getClientInfo(fconfig)); AccumuloInputFormat.setInputTableName(conf, env.getTable()); AccumuloInputFormat.setScanAuthorizations(conf, env.getAuthorizations()); } } catch (Exception e) { throw new RuntimeException(e); } }