/** * Check the last time the specified constraints have been signaled as ready. * * @param viewConstraints The constraints to check for a signal. * * @return the timestamp of the last time the constraints were signaled as ready. * if the constraints have never been signaled, -1 will be returned. * * @throws DatasetException if the signals could not be accessed. */ public long getReadyTimestamp(Constraints viewConstraints) { String normalizedConstraints = getNormalizedConstraints(viewConstraints); Path signalPath = new Path(signalDirectory, normalizedConstraints); // check if the signal exists try { try { FileStatus signalStatus = rootFileSystem.getFileStatus(signalPath); return signalStatus.getModificationTime(); } catch (final FileNotFoundException ex) { // empty, will be thrown when the signal path doesn't exist } return -1; } catch (IOException e) { throw new DatasetIOException("Could not access signal path: " + signalPath, e); } }
/** * Create a signal for the specified constraints. * * @param viewConstraints The constraints to create a signal for. * * @throws DatasetException if the signal could not be created. */ public void signalReady(Constraints viewConstraints) { try { rootFileSystem.mkdirs(signalDirectory); } catch (IOException e) { throw new DatasetIOException("Unable to create signal manager directory: " + signalDirectory, e); } String normalizedConstraints = getNormalizedConstraints(viewConstraints); Path signalPath = new Path(signalDirectory, normalizedConstraints); try{ // create the output stream to overwrite the current contents, if the directory or file // exists it will be overwritten to get a new timestamp FSDataOutputStream os = rootFileSystem.create(signalPath, true); os.close(); } catch (IOException e) { throw new DatasetIOException("Could not access signal path: " + signalPath, e); } }
@Test public void testNormalizeConstraintsUnbounded() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("unbounded", normalizedConstraints); }
@Test public void testConstraintsSignaledReady() throws IOException { SignalManager manager = new SignalManager(fileSystem, testDirectory); Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA). with("email", "testConstraintsSignaledReady@domain.com"); manager.signalReady(constraints); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertTrue(this.fileSystem.exists(new Path(this.testDirectory, normalizedConstraints))); }
@Test public void testNormalizeConstraintsValueExists() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA). with("email", ""); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("email=in()", normalizedConstraints); }
@Test public void testNormalizeConstraintsOrderedSets() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.OLD_VALUE_SCHEMA). with("value", 7L,2L,3L); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("value=2,3,7", normalizedConstraints); }
@Test public void testNormalizeConstraints() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA). with("email", "user@domain.com"); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("email=user%40domain.com", normalizedConstraints); }
@Test public void testNormalizeConstraintsIntervals() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.OLD_VALUE_SCHEMA). toBefore("value", 12L); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("value=(,12)", normalizedConstraints); }
@Test public void testMultiConstraintsSignaledReady() throws IOException { SignalManager manager = new SignalManager(fileSystem, testDirectory); Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA). with("email", "kiteuser@domain.com").with("username", "kiteuser"); manager.signalReady(constraints); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertTrue(this.fileSystem.exists(new Path(this.testDirectory, normalizedConstraints))); }
@Test public void testConstraintsSignaledReadyPreviouslySignaled() throws IOException, InterruptedException { SignalManager manager = new SignalManager(fileSystem, testDirectory); Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA) .with("email", "testConstraintsSignaledReadyPreviouslySignaled@domain.com"); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Path signalFilePath = new Path(this.testDirectory,normalizedConstraints); manager.signalReady(constraints); Assert.assertTrue(this.fileSystem.exists(signalFilePath)); long firstSignalContents = this.fileSystem.getFileStatus(signalFilePath).getModificationTime(); // the modified time depends on the filesystem, and may only be granular to the second // signal and check until the modified time is after the current time, or until // enough time has past that the signal should have been distinguishable long spinStart = System.currentTimeMillis(); long signaledTime = 0; while(firstSignalContents >= signaledTime && (System.currentTimeMillis() - spinStart) <= 2000) { manager.signalReady(constraints); signaledTime = manager.getReadyTimestamp(constraints); } Assert.assertFalse("Second signal should not match the first", signaledTime == firstSignalContents); }
@Test public void testNormalizeConstraintsOrderedKeys() throws IOException { Constraints constraints = new Constraints(DatasetTestUtilities.USER_SCHEMA). with("username", "kite").with("email", "kite@domain.com"); String normalizedConstraints = SignalManager.getNormalizedConstraints(constraints); Assert.assertEquals("email=kite%40domain.com&username=kite", normalizedConstraints); }