/** * Get milliseconds since epoch; {@code read(MILLISECONDS}). * * @return milliseconds since epoch; {@code read(MILLISECONDS}). */ public long millis() { return read(MILLISECONDS); }
/** * Get clock instance preferred by this system. * * @return clock instance preferred by this system. * @since 4.6 */ public MonotonicClock getClock() { return new MonotonicSystemClock(); }
/** {@inheritDoc} */ @Override public ProposedTimestamp propose() { final long u = nowMicros(); return new ProposedTimestamp() { @Override public long read(TimeUnit unit) { return unit.convert(u, MICROSECONDS); } @Override public void blockUntil(Duration maxWait) { // Assume system clock never goes backwards. } }; } }
throw new TimeoutException(); itr.next().blockUntil(Duration.ofMillis(w)); if (itr.hasNext()) { now = System.currentTimeMillis();
@Override public void run() { MonotonicClock clk = getSystem().getClock(); try (Repository git = getLeader().openRepository(); ProposedTimestamp ts = clk.propose()) { try { update(git, req, ts); req.done(git); } catch (Throwable err) { req.setException(git, err); } } catch (IOException err) { req.setException(null, err); } } });
/** * Construct a new {@link org.eclipse.jgit.lib.PersonIdent} with current * time. * * @param aName * a {@link java.lang.String} object. * @param aEmailAddress * a {@link java.lang.String} object. * @param when * a {@link org.eclipse.jgit.util.time.ProposedTimestamp} object. * @since 4.6 */ public PersonIdent(String aName, String aEmailAddress, ProposedTimestamp when) { this(aName, aEmailAddress, when.millis()); }
/** {@inheritDoc} */ @Override public String toString() { return instant().toString(); } }
/** * Get time since epoch, with up to microsecond resolution. * * @return time since epoch, with up to microsecond resolution. */ public Instant instant() { long usec = micros(); long secs = usec / 1000000L; long nanos = (usec % 1000000L) * 1000L; return Instant.ofEpochSecond(secs, nanos); }
/** * Get microseconds since epoch; {@code read(MICROSECONDS}). * * @return microseconds since epoch; {@code read(MICROSECONDS}). */ public long micros() { return read(MICROSECONDS); }
private void blockUntil(ProposedTimestamp ts) throws IOException { try { ts.blockUntil(getSystem().getMaxWaitForMonotonicClock()); } catch (InterruptedException | TimeoutException e) { throw new TimeIsUncertainException(e); } } }
/** * Create a default system with a thread pool of 1 thread per CPU. */ public KetchSystem() { this(defaultExecutor(), new MonotonicSystemClock(), DEFAULT_TXN_NAMESPACE); }
@Override void start() throws IOException { ObjectId id; try (Repository git = leader.openRepository(); ProposedTimestamp ts = getSystem().getClock().propose(); ObjectInserter inserter = git.newObjectInserter()) { id = bumpTerm(git, ts, inserter); inserter.flush(); blockUntil(ts); } runAsync(id); }
/** * Get time since epoch, with up to millisecond resolution. * * @return time since epoch, with up to millisecond resolution. */ public Date date() { return new Date(millis()); }
/** * Get time since epoch, with up to microsecond resolution. * * @return time since epoch, with up to microsecond resolution. */ public Timestamp timestamp() { return Timestamp.from(instant()); }
/** * Get microseconds since epoch; {@code read(MICROSECONDS}). * * @return microseconds since epoch; {@code read(MICROSECONDS}). */ public long micros() { return read(MICROSECONDS); }
private void blockUntil(ProposedTimestamp ts) throws TimeIsUncertainException { List<ProposedTimestamp> times = todo.stream() .flatMap(p -> p.getProposedTimestamps().stream()) .collect(Collectors.toCollection(ArrayList::new)); times.add(ts); try { Duration maxWait = getSystem().getMaxWaitForMonotonicClock(); ProposedTimestamp.blockUntil(times, maxWait); } catch (InterruptedException | TimeoutException e) { throw new TimeIsUncertainException(e); } }
/** * Get clock instance preferred by this system. * * @return clock instance preferred by this system. * @since 4.6 */ public MonotonicClock getClock() { return new MonotonicSystemClock(); }
@Override void start() throws IOException { for (Proposal p : todo) { p.notifyState(RUNNING); } try { ObjectId id; try (Repository git = leader.openRepository(); ProposedTimestamp ts = getSystem().getClock().propose()) { id = insertProposals(git, ts); blockUntil(ts); } runAsync(id); } catch (NoOp e) { for (Proposal p : todo) { p.success(); } leader.lock.lock(); try { leader.nextRound(); } finally { leader.lock.unlock(); } } catch (IOException e) { abort(); throw e; } }
RevCommit c = rw.parseCommit(acceptedOldIndex); if (getSystem().requireMonotonicLeaderElections()) { if (ts.read(SECONDS) < c.getCommitTime()) { throw new TimeIsUncertainException();
/** * Wait for timestamps to be in the past, aborting commands on timeout. * * @param maxWait * maximum amount of time to wait for timestamps to resolve. * @return true if timestamps were successfully waited for; false if * commands were aborted. * @since 4.6 */ protected boolean blockUntilTimestamps(Duration maxWait) { if (timestamps == null) { return true; } try { ProposedTimestamp.blockUntil(timestamps, maxWait); return true; } catch (TimeoutException | InterruptedException e) { String msg = JGitText.get().timeIsUncertain; for (ReceiveCommand c : commands) { if (c.getResult() == NOT_ATTEMPTED) { c.setResult(REJECTED_OTHER_REASON, msg); } } return false; } }