private void addIntervalToTimeline( Interval interval, TimelineEntry entry, NavigableMap<Interval, TimelineEntry> timeline ) { if (interval != null && interval.toDurationMillis() > 0) { timeline.put(interval, entry); } }
protected AbstractFixedIntervalTask( String id, String groupId, TaskResource taskResource, String dataSource, Interval interval, Map<String, Object> context ) { super(id, groupId, taskResource, dataSource, context); this.interval = Preconditions.checkNotNull(interval, "interval"); Preconditions.checkArgument(interval.toDurationMillis() > 0, "interval empty"); }
static boolean isReady(TaskActionClient actionClient, SortedSet<Interval> intervals) throws IOException { // Sanity check preventing empty intervals (which cannot be locked, and don't make sense anyway). for (Interval interval : intervals) { if (interval.toDurationMillis() == 0) { throw new ISE("Cannot run with empty interval[%s]", interval); } } final List<TaskLock> locks = getTaskLocks(actionClient); if (locks.size() == 0) { try { Tasks.tryAcquireExclusiveLocks(actionClient, intervals); } catch (Exception e) { return false; } } return true; }
public static List<Granularity> granularitiesFinerThan(final Granularity gran0) { final List<Granularity> retVal = new ArrayList<>(); final DateTime origin = (gran0 instanceof PeriodGranularity) ? ((PeriodGranularity) gran0).getOrigin() : null; final DateTimeZone tz = (gran0 instanceof PeriodGranularity) ? ((PeriodGranularity) gran0).getTimeZone() : null; for (GranularityType gran : GranularityType.values()) { /** * All and None are excluded b/c when asked to give all granularities finer * than "TEN_MINUTE", you want the answer to be "FIVE_MINUTE, MINUTE and SECOND" * it doesn't make sense to include ALL or None to be part of this. */ if (gran == GranularityType.ALL || gran == GranularityType.NONE) { continue; } final Granularity segmentGranularity = gran.create(origin, tz); final long segmentGranularityDurationMillis = segmentGranularity.bucket(DateTimes.EPOCH).toDurationMillis(); final long gran0DurationMillis = gran0.bucket(DateTimes.EPOCH).toDurationMillis(); if (segmentGranularityDurationMillis <= gran0DurationMillis) { retVal.add(segmentGranularity); } } retVal.sort((g1, g2) -> { long duration1 = g2.bucket(DateTimes.EPOCH).toDurationMillis(); long duration2 = g1.bucket(DateTimes.EPOCH).toDurationMillis(); return Longs.compare(duration1, duration2); }); return retVal; }
Preconditions.checkArgument(interval == null || segments == null, "one of interval and segments should be null"); if (interval != null && interval.toDurationMillis() == 0) { throw new IAE("Interval[%s] is empty, must specify a nonempty interval", interval);
/** * Get a connection, or throw an exception if it doesn't exist. Also refreshes the timeout timer. * * @param connectionId connection id * * @return the connection * * @throws NoSuchConnectionException if the connection id doesn't exist */ @Nonnull private DruidConnection getDruidConnection(final String connectionId) { final DruidConnection connection = connections.get(connectionId); if (connection == null) { throw new NoSuchConnectionException(connectionId); } return connection.sync( exec.schedule( () -> { log.debug("Connection[%s] timed out.", connectionId); closeConnection(new ConnectionHandle(connectionId)); }, new Interval(DateTimes.nowUtc(), config.getConnectionIdleTimeout()).toDurationMillis(), TimeUnit.MILLISECONDS ) ); }
/** * Creates a new interval with the specified duration after the start instant. * * @param duration the duration to add to the start to get the new end instant, null means zero * @return an interval with the start from this interval and a calculated end * @throws IllegalArgumentException if the duration is negative */ public Interval withDurationAfterStart(ReadableDuration duration) { long durationMillis = DateTimeUtils.getDurationMillis(duration); if (durationMillis == toDurationMillis()) { return this; } Chronology chrono = getChronology(); long startMillis = getStartMillis(); long endMillis = chrono.add(startMillis, durationMillis, 1); return new Interval(startMillis, endMillis, chrono); }
/** * Creates a new interval with the specified duration before the end instant. * * @param duration the duration to subtract from the end to get the new start instant, null means zero * @return an interval with the end from this interval and a calculated start * @throws IllegalArgumentException if the duration is negative */ public Interval withDurationBeforeEnd(ReadableDuration duration) { long durationMillis = DateTimeUtils.getDurationMillis(duration); if (durationMillis == toDurationMillis()) { return this; } Chronology chrono = getChronology(); long endMillis = getEndMillis(); long startMillis = chrono.add(endMillis, durationMillis, -1); return new Interval(startMillis, endMillis, chrono); }
/** * Creates a new interval with the specified duration before the end instant. * * @param duration the duration to subtract from the end to get the new start instant, null means zero * @return an interval with the end from this interval and a calculated start * @throws IllegalArgumentException if the duration is negative */ public Interval withDurationBeforeEnd(ReadableDuration duration) { long durationMillis = DateTimeUtils.getDurationMillis(duration); if (durationMillis == toDurationMillis()) { return this; } Chronology chrono = getChronology(); long endMillis = getEndMillis(); long startMillis = chrono.add(endMillis, durationMillis, -1); return new Interval(startMillis, endMillis, chrono); }
@BenchmarkOptions(warmupRounds = 1000, benchmarkRounds = 1000000) @Test public void testJodaGap() { long diff = 0; for (int i = 0; i < 1000; i++) { diff = diff + interval1.gap(interval2).toDurationMillis(); } sum = diff; } }
/** * Creates a new interval with the specified duration after the start instant. * * @param duration the duration to add to the start to get the new end instant, null means zero * @return an interval with the start from this interval and a calculated end * @throws IllegalArgumentException if the duration is negative */ public Interval withDurationAfterStart(ReadableDuration duration) { long durationMillis = DateTimeUtils.getDurationMillis(duration); if (durationMillis == toDurationMillis()) { return this; } Chronology chrono = getChronology(); long startMillis = getStartMillis(); long endMillis = chrono.add(startMillis, durationMillis, 1); return new Interval(startMillis, endMillis, chrono); }
public static int D(final String dayString) { return (int) (Intervals.utc(T("1970"), T(dayString)).toDurationMillis() / (86400L * 1000L)); }
final long timeout = new Interval(start, taskConfig.getGracefulShutdownTimeout()).toDurationMillis();
ArrayList<Long> actualIntervals = new ArrayList<>(); for (Interval interval : intervals) { actualIntervals.add(interval.toDurationMillis()); new Interval("2012-01-01/2012-01-02", chrono).toDurationMillis(), new Interval("2012-01-02/2012-01-03", chrono).toDurationMillis(), new Interval("2012-01-03/2012-01-04", chrono).toDurationMillis(), new Interval("2012-01-07/2012-01-08", chrono).toDurationMillis(), new Interval("2012-01-08/2012-01-09", chrono).toDurationMillis(), new Interval("2012-01-09/2012-01-10", chrono).toDurationMillis(), new Interval("2012-01-10/2012-01-11", chrono).toDurationMillis(), new Interval("2012-09-01/2012-09-02", chrono).toDurationMillis(), new Interval("2012-09-02/2012-09-03", chrono).toDurationMillis() );
@Test(groups = "slow", expectedExceptions = SubscriptionBaseApiException.class) public void testCancelSubscriptionWithInvalidRequestedDate() throws SubscriptionBaseApiException { final String prod = "Shotgun"; final BillingPeriod term = BillingPeriod.MONTHLY; final String planSet = PriceListSet.DEFAULT_PRICELIST_NAME; // CREATE final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, prod, term, planSet); PlanPhase currentPhase = subscription.getCurrentPhase(); assertEquals(currentPhase.getPhaseType(), PhaseType.TRIAL); // MOVE TO NEXT PHASE testListener.pushExpectedEvent(NextEvent.PHASE); final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31)); clock.addDeltaFromReality(it.toDurationMillis()); assertListenerStatus(); currentPhase = subscription.getCurrentPhase(); assertEquals(currentPhase.getPhaseType(), PhaseType.EVERGREEN); final DateTime invalidDate = subscription.getBundleStartDate().minusDays(3); // CANCEL in EVERGREEN period with an invalid Date (prior to the Creation Date) subscription.cancelWithDate(invalidDate, callContext); }
@Test(groups = "slow") public void testInvalidChangesAcrossProductTypes() throws SubscriptionBaseApiException { final String baseProduct = "Shotgun"; final BillingPeriod baseTerm = BillingPeriod.MONTHLY; final String basePriceList = PriceListSet.DEFAULT_PRICELIST_NAME; // CREATE BP final DefaultSubscriptionBase baseSubscription = testUtil.createSubscription(bundle, baseProduct, baseTerm, basePriceList); // MOVE CLOCK 14 DAYS LATER Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(14)); clock.addDeltaFromReality(it.toDurationMillis()); // Create AO final String aoProduct = "Laser-Scope"; final BillingPeriod aoTerm = BillingPeriod.MONTHLY; final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME; DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList); try { final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(baseProduct, baseTerm, basePriceList); aoSubscription.changePlanWithDate(new DefaultEntitlementSpecifier(planPhaseSpecifier), clock.getUTCNow(), callContext); Assert.fail("Should not allow plan change across product type"); } catch (final SubscriptionBaseApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_INVALID.getCode()); } }
task.stopGracefully(taskConfig); final TaskStatus taskStatus = item.getResult().get( new Interval(DateTimes.utc(start), taskConfig.getGracefulShutdownTimeout()).toDurationMillis(), TimeUnit.MILLISECONDS );
private void tChangePlanBundleAlignIMM(final String fromProd, final BillingPeriod fromTerm, final String fromPlanSet, final String toProd, final BillingPeriod toTerm, final String toPlanSet) throws SubscriptionBaseApiException { final DefaultSubscriptionBase subscription = testUtil.createSubscription(bundle, fromProd, fromTerm, fromPlanSet); testListener.pushExpectedEvent(NextEvent.CHANGE); Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(3)); clock.addDeltaFromReality(it.toDurationMillis()); // CHANGE PLAN IMM final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier(toProd, toTerm, toPlanSet); subscription.changePlan(new DefaultEntitlementSpecifier(planPhaseSpecifier), callContext); checkChangePlan(subscription, toProd, ProductCategory.BASE, toTerm, PhaseType.TRIAL); assertListenerStatus(); final PlanPhase currentPhase = subscription.getCurrentPhase(); final DateTime nextExpectedPhaseChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), currentPhase.getDuration()); testUtil.checkNextPhaseChange(subscription, 1, nextExpectedPhaseChange); // NEXT PHASE testListener.pushExpectedEvent(NextEvent.PHASE); it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(30)); clock.addDeltaFromReality(it.toDurationMillis()); final DateTime futureNow = clock.getUTCNow(); assertTrue(futureNow.isAfter(nextExpectedPhaseChange)); assertListenerStatus(); }
throw new ISE("Unable to grant lock to inactive Task [%s]", task.getId()); Preconditions.checkArgument(interval.toDurationMillis() > 0, "interval empty");
@Test(groups = "fast") public void testChangeSubscriptionFutureCancelled() throws SubscriptionBaseApiException { SubscriptionBase subscription = testUtil.createSubscription(bundle, "Shotgun", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); final PlanPhase trialPhase = subscription.getCurrentPhase(); // MOVE TO NEXT PHASE final PlanPhase currentPhase = subscription.getCurrentPhase(); testListener.pushExpectedEvent(NextEvent.PHASE); final Interval it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusDays(31)); clock.addDeltaFromReality(it.toDurationMillis()); assertListenerStatus(); // SET CTD TO CANCEL IN FUTURE final DateTime expectedPhaseTrialChange = TestSubscriptionHelper.addDuration(subscription.getStartDate(), trialPhase.getDuration()); final Duration ctd = testUtil.getDurationMonth(1); final DateTime newChargedThroughDate = TestSubscriptionHelper.addDuration(expectedPhaseTrialChange, ctd); subscriptionInternalApi.setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext); subscription = subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext); subscription.cancelWithPolicy(BillingActionPolicy.END_OF_TERM, callContext); try { final PlanPhaseSpecifier planPhaseSpecifier = new PlanPhaseSpecifier("Pistol", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME); subscription.changePlanWithDate(new DefaultEntitlementSpecifier(planPhaseSpecifier), clock.getUTCNow(), callContext); Assert.fail("Exception expected, error code: " + ErrorCode.SUB_CHANGE_FUTURE_CANCELLED); } catch (final SubscriptionBaseApiException e) { assertEquals(e.getCode(), ErrorCode.SUB_CHANGE_FUTURE_CANCELLED.getCode()); } assertListenerStatus(); }