/** * Formats time elapsed since the given start time (in milliseconds). * * @param startTimeMs start time in milliseconds * @param message prefix for the message to be printed * @return formatted string with the elapsed time (in milliseconds) */ public static String formatTimeTakenMs(long startTimeMs, String message) { return message + " took " + (CommonUtils.getCurrentMs() - startTimeMs) + " ms."; }
@Override public boolean attempt() { if (mFirstAttempt || (CommonUtils.getCurrentMs() - mLastAttempTimeMs) > mRefreshPeriodMs) { mLastAttempTimeMs = CommonUtils.getCurrentMs(); mFirstAttempt = false; return true; } return false; } }
/** * Creates a new instance. */ public JobIdGenerator() { // shift by 10,000 to avoid conflicts with worker IDs. mNextJobId = new AtomicLong(CommonUtils.getCurrentMs() + 10 * 1000); }
/** * @param status the job status */ public synchronized void setStatus(Status status) { Status oldStatus = mStatus; mStatus = status; mLastStatusChangeMs = CommonUtils.getCurrentMs(); if (mStatusChangeCallback != null && status != oldStatus) { mStatusChangeCallback.apply(this); } }
/** * Creates a new instance of {@link JobInfo}. * * @param id the job id * @param jobConfig the job configuration * @param statusChangeCallback the callback to invoke upon status change */ public JobInfo(long id, JobConfig jobConfig, Function<JobInfo, Void> statusChangeCallback) { mId = id; mJobConfig = Preconditions.checkNotNull(jobConfig); mTaskIdToInfo = Maps.newHashMap(); mLastStatusChangeMs = CommonUtils.getCurrentMs(); mErrorMessage = ""; mStatus = Status.CREATED; mStatusChangeCallback = statusChangeCallback; }
@Override public boolean attempt() { if (mAttemptCount == 0) { // first attempt, set the start time mStartMs = CommonUtils.getCurrentMs(); mAttemptCount++; return true; } if (mSleepMs > 0) { CommonUtils.sleepMs(mSleepMs); } if ((CommonUtils.getCurrentMs() - mStartMs) <= mRetryTimeoutMs) { mAttemptCount++; return true; } return false; } }
private void write(FileSystem alluxioClient) throws IOException, AlluxioException { FileOutStream fileOutStream = createFile(alluxioClient, mFilePath, mDeleteIfExists); long startTimeMs = CommonUtils.getCurrentMs(); try (DataOutputStream os = new DataOutputStream(fileOutStream)) { os.writeInt(mLength); for (int i = 0; i < mLength; i++) { os.writeInt(i); } } LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "writeFile to file " + mFilePath)); }
private void writeFile(FileSystem fileSystem) throws IOException, AlluxioException { ByteBuffer buf = ByteBuffer.allocate(NUMBERS * 4); buf.order(ByteOrder.nativeOrder()); for (int k = 0; k < NUMBERS; k++) { buf.putInt(k); } LOG.debug("Writing data..."); long startTimeMs = CommonUtils.getCurrentMs(); FileOutStream os = fileSystem.createFile(mFilePath, mWriteOptions); os.write(buf.array()); os.close(); LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "writeFile to file " + mFilePath)); }
private boolean readFile(FileSystem fileSystem) throws IOException, AlluxioException { boolean pass = true; LOG.debug("Reading data..."); final long startTimeMs = CommonUtils.getCurrentMs(); FileInStream is = fileSystem.openFile(mFilePath, mReadOptions); ByteBuffer buf = ByteBuffer.allocate((int) is.remaining()); is.read(buf.array()); buf.order(ByteOrder.nativeOrder()); for (int k = 0; k < NUMBERS; k++) { pass = pass && (buf.getInt() == k); } is.close(); LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "readFile file " + mFilePath)); return pass; } }
private boolean read(FileSystem alluxioClient) throws IOException, AlluxioException { OpenFilePOptions options = OpenFilePOptions.newBuilder().setReadType(mReadType.toProto()).build(); boolean pass = true; long startTimeMs = CommonUtils.getCurrentMs(); try (DataInputStream input = new DataInputStream(alluxioClient.openFile(mFilePath, options))) { int length = input.readInt(); for (int i = 0; i < length; i++) { if (input.readInt() != i) { pass = false; break; } } } LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "readFile file " + mFilePath)); return pass; } }
while (jobIterator.hasNext()) { JobInfo oldestJob = jobIterator.next(); long completedBeforeMs = CommonUtils.getCurrentMs() - oldestJob.getLastStatusChangeMs(); if (completedBeforeMs < RETENTION_MS) {
/** * Tests the {@link CommonUtils#getCurrentMs()} and {@link CommonUtils#sleepMs(long)} methods. */ @Test public void getCurrentMsAndSleepMs() { long delta = 100; long startTime = CommonUtils.getCurrentMs(); CommonUtils.sleepMs(delta); long currentTime = CommonUtils.getCurrentMs(); /* Check that currentTime falls into the interval [startTime + delta; startTime + 2*delta] */ assertTrue(startTime + delta <= currentTime); assertTrue(currentTime <= 2 * delta + startTime); }
/** * @return generated {@link WorkerInfo} for this worker */ public synchronized WorkerInfo generateClientWorkerInfo() { return new WorkerInfo().setId(mId).setAddress(mWorkerAddress).setLastContactSec( (int) ((CommonUtils.getCurrentMs() - mLastUpdatedTimeMs) / Constants.SECOND_MS)) .setState("In Service").setStartTimeMs(mStartTimeMs); }
/** * Tests the {@link FormatUtils#formatTimeTakenMs(long, String)} method. */ @Test public void formatTimeTakenMs() { class TestCase { Pattern mExpected; String mInputMessage; public TestCase(String expectedRE, String inputMessage) { mExpected = Pattern.compile(expectedRE); mInputMessage = inputMessage; } } List<TestCase> testCases = new ArrayList<>(); testCases.add(new TestCase("^Task A took (.*) ms.$", "Task A")); testCases.add(new TestCase("^Task B took (.*) ms.$", "Task B")); long delta = 100; for (TestCase testCase : testCases) { String result = FormatUtils.formatTimeTakenMs(CommonUtils.getCurrentMs() - delta, testCase.mInputMessage); Matcher match = testCase.mExpected.matcher(result); Assert.assertTrue(match.matches()); Assert.assertTrue(delta <= Long.parseLong(match.group(1))); Assert.assertTrue(Long.parseLong(match.group(1)) <= 2 * delta); } }
case LAST_CONTACT_SEC: info.setLastContactSec( (int) ((CommonUtils.getCurrentMs() - mLastUpdatedTimeMs) / Constants.SECOND_MS)); break; case START_TIME_MS:
/** * Formats time elapsed since the given start time (in milliseconds). * * @param startTimeMs start time in milliseconds * @param message prefix for the message to be printed * @return formatted string with the elapsed time (in milliseconds) */ public static String formatTimeTakenMs(long startTimeMs, String message) { return message + " took " + (CommonUtils.getCurrentMs() - startTimeMs) + " ms."; }
private void write(FileSystem alluxioClient) throws IOException, AlluxioException { FileOutStream fileOutStream = createFile(alluxioClient, mFilePath, mDeleteIfExists); long startTimeMs = CommonUtils.getCurrentMs(); try (DataOutputStream os = new DataOutputStream(fileOutStream)) { os.writeInt(mLength); for (int i = 0; i < mLength; i++) { os.writeInt(i); } } LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "writeFile to file " + mFilePath)); }
private void writeFile(FileSystem fileSystem) throws IOException, AlluxioException { ByteBuffer buf = ByteBuffer.allocate(NUMBERS * 4); buf.order(ByteOrder.nativeOrder()); for (int k = 0; k < NUMBERS; k++) { buf.putInt(k); } LOG.debug("Writing data..."); long startTimeMs = CommonUtils.getCurrentMs(); FileOutStream os = fileSystem.createFile(mFilePath, mWriteOptions); os.write(buf.array()); os.close(); LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "writeFile to file " + mFilePath)); }
private boolean read(FileSystem alluxioClient) throws IOException, AlluxioException { OpenFileOptions options = OpenFileOptions.defaults().setReadType(mReadType); boolean pass = true; long startTimeMs = CommonUtils.getCurrentMs(); try (DataInputStream input = new DataInputStream(alluxioClient.openFile(mFilePath, options))) { int length = input.readInt(); for (int i = 0; i < length; i++) { if (input.readInt() != i) { pass = false; break; } } } LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "readFile file " + mFilePath)); return pass; } }
/** * Creates the files for this example. */ public static void createFiles() throws AlluxioException, IOException { final long startTimeMs = CommonUtils.getCurrentMs(); for (int k = 0; k < sFiles; k++) { sFileSystem.createFile(new AlluxioURI(sFileName + (k + sBaseFileNumber))).close(); LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "createFile")); } }