List of usage examples for org.joda.time Interval getStart
public DateTime getStart()
From source file:com.metamx.druid.realtime.plumber.IntervalStartVersioningPolicy.java
License:Open Source License
@Override public String getVersion(Interval interval) { return interval.getStart().toString(); }
From source file:com.metamx.druid.utils.ExposeS3DataSource.java
License:Open Source License
public static void main(String[] args) throws ServiceException, IOException, NoSuchAlgorithmException { CLI cli = new CLI(); cli.addOption(new RequiredOption(null, "s3Bucket", true, "s3 bucket to pull data from")); cli.addOption(new RequiredOption(null, "s3Path", true, "base input path in s3 bucket. Everything until the date strings.")); cli.addOption(new RequiredOption(null, "timeInterval", true, "ISO8601 interval of dates to index")); cli.addOption(new RequiredOption(null, "granularity", true, String.format( "granularity of index, supported granularities: [%s]", Arrays.asList(Granularity.values())))); cli.addOption(new RequiredOption(null, "zkCluster", true, "Cluster string to connect to ZK with.")); cli.addOption(new RequiredOption(null, "zkBasePath", true, "The base path to register index changes to.")); CommandLine commandLine = cli.parse(args); if (commandLine == null) { return;// w ww . ja v a 2 s.c o m } String s3Bucket = commandLine.getOptionValue("s3Bucket"); String s3Path = commandLine.getOptionValue("s3Path"); String timeIntervalString = commandLine.getOptionValue("timeInterval"); String granularity = commandLine.getOptionValue("granularity"); String zkCluster = commandLine.getOptionValue("zkCluster"); String zkBasePath = commandLine.getOptionValue("zkBasePath"); Interval timeInterval = new Interval(timeIntervalString); Granularity gran = Granularity.valueOf(granularity.toUpperCase()); final RestS3Service s3Client = new RestS3Service(new AWSCredentials( System.getProperty("com.metamx.aws.accessKey"), System.getProperty("com.metamx.aws.secretKey"))); ZkClient zkClient = new ZkClient(new ZkConnection(zkCluster), Integer.MAX_VALUE, new StringZkSerializer()); zkClient.waitUntilConnected(); for (Interval interval : gran.getIterable(timeInterval)) { log.info("Processing interval[%s]", interval); String s3DatePath = JOINER.join(s3Path, gran.toPath(interval.getStart())); if (!s3DatePath.endsWith("/")) { s3DatePath += "/"; } StorageObjectsChunk chunk = s3Client.listObjectsChunked(s3Bucket, s3DatePath, "/", 2000, null, true); TreeSet<String> commonPrefixes = Sets.newTreeSet(); commonPrefixes.addAll(Arrays.asList(chunk.getCommonPrefixes())); if (commonPrefixes.isEmpty()) { log.info("Nothing at s3://%s/%s", s3Bucket, s3DatePath); continue; } String latestPrefix = commonPrefixes.last(); log.info("Latest segments at [s3://%s/%s]", s3Bucket, latestPrefix); chunk = s3Client.listObjectsChunked(s3Bucket, latestPrefix, "/", 2000, null, true); Integer partitionNumber; if (chunk.getCommonPrefixes().length == 0) { partitionNumber = null; } else { partitionNumber = -1; for (String partitionPrefix : chunk.getCommonPrefixes()) { String[] splits = partitionPrefix.split("/"); partitionNumber = Math.max(partitionNumber, Integer.parseInt(splits[splits.length - 1])); } } log.info("Highest segment partition[%,d]", partitionNumber); if (partitionNumber == null) { final S3Object s3Obj = new S3Object(new S3Bucket(s3Bucket), String.format("%sdescriptor.json", latestPrefix)); updateWithS3Object(zkBasePath, s3Client, zkClient, s3Obj); } else { for (int i = partitionNumber; i >= 0; --i) { final S3Object partitionObject = new S3Object(new S3Bucket(s3Bucket), String.format("%s%s/descriptor.json", latestPrefix, i)); updateWithS3Object(zkBasePath, s3Client, zkClient, partitionObject); } } } }
From source file:com.metamx.druid.VersionedIntervalTimeline.java
License:Open Source License
private boolean addAtKey(NavigableMap<Interval, TimelineEntry> timeline, Interval key, TimelineEntry entry) { boolean retVal = false; Interval currKey = key; Interval entryInterval = entry.getTrueInterval(); if (!currKey.overlaps(entryInterval)) { return false; }/*from w w w.ja v a2 s . c om*/ while (currKey != null && currKey.overlaps(entryInterval)) { Interval nextKey = timeline.higherKey(currKey); int versionCompare = versionComparator.compare(entry.getVersion(), timeline.get(currKey).getVersion()); if (versionCompare < 0) { if (currKey.contains(entryInterval)) { return true; } else if (currKey.getStart().isBefore(entryInterval.getStart())) { entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd()); } else { addIntervalToTimeline(new Interval(entryInterval.getStart(), currKey.getStart()), entry, timeline); if (entryInterval.getEnd().isAfter(currKey.getEnd())) { entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd()); } else { entryInterval = null; } } } else if (versionCompare > 0) { TimelineEntry oldEntry = timeline.remove(currKey); if (currKey.contains(entryInterval)) { addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline); addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline); addIntervalToTimeline(entryInterval, entry, timeline); return true; } else if (currKey.getStart().isBefore(entryInterval.getStart())) { addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline); } else if (entryInterval.getEnd().isBefore(currKey.getEnd())) { addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline); } } else { if (timeline.get(currKey).equals(entry)) { // This occurs when restoring segments timeline.remove(currKey); } else { throw new UnsupportedOperationException( String.format("Cannot add overlapping segments [%s and %s] with the same version [%s]", currKey, entryInterval, entry.getVersion())); } } currKey = nextKey; retVal = true; } addIntervalToTimeline(entryInterval, entry, timeline); return retVal; }
From source file:com.metamx.druid.VersionedIntervalTimeline.java
License:Open Source License
private List<TimelineObjectHolder<VersionType, ObjectType>> lookup(Interval interval, boolean incompleteOk) { List<TimelineObjectHolder<VersionType, ObjectType>> retVal = new ArrayList<TimelineObjectHolder<VersionType, ObjectType>>(); NavigableMap<Interval, TimelineEntry> timeline = (incompleteOk) ? incompletePartitionsTimeline : completePartitionsTimeline; for (Map.Entry<Interval, TimelineEntry> entry : timeline.entrySet()) { Interval timelineInterval = entry.getKey(); TimelineEntry val = entry.getValue(); if (timelineInterval.overlaps(interval)) { retVal.add(new TimelineObjectHolder<VersionType, ObjectType>(timelineInterval, val.getVersion(), val.getPartitionHolder())); }/* w w w . ja v a 2 s. c o m*/ } if (retVal.isEmpty()) { return retVal; } TimelineObjectHolder<VersionType, ObjectType> firstEntry = retVal.get(0); if (interval.overlaps(firstEntry.getInterval()) && interval.getStart().isAfter(firstEntry.getInterval().getStart())) { retVal.set(0, new TimelineObjectHolder<VersionType, ObjectType>( new Interval(interval.getStart(), firstEntry.getInterval().getEnd()), firstEntry.getVersion(), firstEntry.getObject())); } TimelineObjectHolder<VersionType, ObjectType> lastEntry = retVal.get(retVal.size() - 1); if (interval.overlaps(lastEntry.getInterval()) && interval.getEnd().isBefore(lastEntry.getInterval().getEnd())) { retVal.set(retVal.size() - 1, new TimelineObjectHolder<VersionType, ObjectType>( new Interval(lastEntry.getInterval().getStart(), interval.getEnd()), lastEntry.getVersion(), lastEntry.getObject())); } return retVal; }
From source file:com.microsoft.exchange.DateHelp.java
License:Apache License
/** * will always return at least two intervals. * /*from ww w . ja v a 2 s . c om*/ * @param start * @param end * @param count * @return */ public static List<Interval> generateMultipleIntervals(Date start, Date end, int count) { List<Interval> intervals = generateIntervals(start, end); int actualCount = intervals.size(); if (count > actualCount) { while (actualCount < count) { List<Interval> tIntervals = new ArrayList<Interval>(); for (Interval i : intervals) { tIntervals.addAll(generateIntervals(i.getStart().toDate(), i.getEnd().toDate())); } intervals = tIntervals; actualCount = intervals.size(); } } return intervals; }
From source file:com.microsoft.exchange.ExchangeDateUtils.java
License:Apache License
/** * will always return at least two intervals. * /*ww w . j a v a 2 s .c o m*/ * @param start * @param end * @param count * @return */ public static List<Interval> generateMultipleIntervals(Date start, Date end, int count) { List<Interval> intervals = generateIntervals(start, end); int actualCount = intervals.size(); if (count > actualCount) { while (actualCount < count) { List<Interval> tIntervals = new ArrayList<Interval>(); for (Interval i : intervals) { tIntervals.addAll(generateIntervals(i.getStart().toDate(), i.getEnd().toDate())); } intervals = tIntervals; actualCount = intervals.size(); } } return intervals; }
From source file:com.microsoft.exchange.impl.BaseExchangeCalendarDataDao.java
License:Apache License
/** * This method issues a {@link FindItem} request using a {@link CalendarViewType} to obtain identifiers for all {@link CalendarItemType}s between {@code startDate} and {@code endDate} * /*from w ww .j a v a2s .c o m*/ * Note: CalendarView element returns single calendar items and all occurrences. In other words, this method expands recurrence for you. * * @see <a href='http://msdn.microsoft.com/en-us/library/office/aa566107(v=exchg.140).aspx'>FindItem Operation</a> * * @param upn * @param startDate * @param endDate * @param calendarIds - if omitted the primary calendar folder will be targeted * @param depth * @return a never null but possibly empty {@link Set} of {@link ItemIdType} */ private Set<ItemIdType> findCalendarItemIdsInternal(String upn, Date startDate, Date endDate, Collection<FolderIdType> calendarIds, int depth) { Validate.isTrue(StringUtils.isNotBlank(upn), "upn argument cannot be blank"); Validate.notNull(startDate, "startDate argument cannot be null"); Validate.notNull(endDate, "endDate argument cannot be null"); //if folderIds is empty the primary calendar folder will be targeted int newDepth = depth + 1; if (depth > getMaxRetries()) { throw new ExchangeRuntimeException("findCalendarItemIdsInternal(upn=" + upn + ",startDate=" + startDate + ",+endDate=" + endDate + ",...) failed " + getMaxRetries() + " consecutive attempts."); } else { setContextCredentials(upn); FindItem request = getRequestFactory().constructCalendarViewFindCalendarItemIdsByDateRange(startDate, endDate, calendarIds); try { FindItemResponse response = getWebServices().findItem(request); return getResponseUtils().parseFindItemIdResponseNoOffset(response); } catch (ExchangeInvalidUPNRuntimeException e0) { log.warn("findCalendarItemIdsInternal(upn=" + upn + ",startDate=" + startDate + ",+endDate=" + endDate + ",...) ExchangeInvalidUPNRuntimeException. Attempting to resolve valid upn... - failure #" + newDepth); String resolvedUpn = resolveUpn(upn); if (StringUtils.isNotBlank(resolvedUpn) && (!resolvedUpn.equalsIgnoreCase(upn))) { return findCalendarItemIdsInternal(resolvedUpn, startDate, endDate, calendarIds, newDepth); } else { //rethrow throw e0; } } catch (ExchangeExceededFindCountLimitRuntimeException e1) { log.warn("findCalendarItemIdsInternal(upn=" + upn + ",startDate=" + startDate + ",+endDate=" + endDate + ",...) ExceededFindCountLimit splitting request and trying again. - failure #" + newDepth); Set<ItemIdType> foundItems = new HashSet<ItemIdType>(); List<Interval> intervals = ExchangeDateUtils.generateIntervals(startDate, endDate); for (Interval i : intervals) { foundItems.addAll(findCalendarItemIdsInternal(upn, i.getStart().toDate(), i.getEnd().toDate(), calendarIds, newDepth)); } return foundItems; } catch (ExchangeRuntimeException e2) { long backoff = getWaitTimeExp(newDepth); log.warn("findCalendarItemIdsInternal(upn=" + upn + ",startDate=" + startDate + ",+endDate=" + endDate + ",...) - failure #" + newDepth + ". Sleeping for " + backoff + " before retry. " + e2.getMessage()); try { Thread.sleep(backoff); } catch (InterruptedException e1) { log.warn("InterruptedException=" + e1); } return findCalendarItemIdsInternal(upn, startDate, endDate, calendarIds, newDepth); } } }
From source file:com.netflix.ice.basic.BasicDataManager.java
License:Apache License
private double[] getData(Interval interval, TagLists tagLists) throws ExecutionException { DateTime start = config.startDate;//from ww w . ja v a 2 s .co m DateTime end = config.startDate; if (consolidateType == ConsolidateType.hourly) { start = interval.getStart().withDayOfMonth(1).withMillisOfDay(0); end = interval.getEnd(); } else if (consolidateType == ConsolidateType.daily) { start = interval.getStart().withDayOfYear(1).withMillisOfDay(0); end = interval.getEnd(); } int num = 0; if (consolidateType == ConsolidateType.hourly) { num = interval.toPeriod(PeriodType.hours()).getHours(); if (interval.getStart().plusHours(num).isBefore(interval.getEnd())) num++; } else if (consolidateType == ConsolidateType.daily) { num = interval.toPeriod(PeriodType.days()).getDays(); if (interval.getStart().plusDays(num).isBefore(interval.getEnd())) num++; } else if (consolidateType == ConsolidateType.weekly) { num = interval.toPeriod(PeriodType.weeks()).getWeeks(); if (interval.getStart().plusWeeks(num).isBefore(interval.getEnd())) num++; } else if (consolidateType == ConsolidateType.monthly) { num = interval.toPeriod(PeriodType.months()).getMonths(); if (interval.getStart().plusMonths(num).isBefore(interval.getEnd())) num++; } double[] result = new double[num]; do { ReadOnlyData data = getReadOnlyData(start); int resultIndex = 0; int fromIndex = 0; if (interval.getStart().isBefore(start)) { if (consolidateType == ConsolidateType.hourly) { resultIndex = Hours.hoursBetween(interval.getStart(), start).getHours(); } else if (consolidateType == ConsolidateType.daily) { resultIndex = Days.daysBetween(interval.getStart(), start).getDays(); } else if (consolidateType == ConsolidateType.weekly) { resultIndex = Weeks.weeksBetween(interval.getStart(), start).getWeeks(); } else if (consolidateType == ConsolidateType.monthly) { resultIndex = Months.monthsBetween(interval.getStart(), start).getMonths(); } } else { if (consolidateType == ConsolidateType.hourly) { fromIndex = Hours.hoursBetween(start, interval.getStart()).getHours(); } else if (consolidateType == ConsolidateType.daily) { fromIndex = Days.daysBetween(start, interval.getStart()).getDays(); } else if (consolidateType == ConsolidateType.weekly) { fromIndex = Weeks.weeksBetween(start, interval.getStart()).getWeeks(); if (start.getDayOfWeek() != interval.getStart().getDayOfWeek()) fromIndex++; } else if (consolidateType == ConsolidateType.monthly) { fromIndex = Months.monthsBetween(start, interval.getStart()).getMonths(); } } List<Integer> columeIndexs = Lists.newArrayList(); int columeIndex = 0; for (TagGroup tagGroup : data.getTagGroups()) { if (tagLists.contains(tagGroup)) columeIndexs.add(columeIndex); columeIndex++; } while (resultIndex < num && fromIndex < data.getNum()) { double[] fromData = data.getData(fromIndex++); for (Integer cIndex : columeIndexs) result[resultIndex] += fromData[cIndex]; resultIndex++; } if (consolidateType == ConsolidateType.hourly) start = start.plusMonths(1); else if (consolidateType == ConsolidateType.daily) start = start.plusYears(1); else break; } while (start.isBefore(end)); return result; }
From source file:com.netflix.ice.basic.BasicThroughputMetricService.java
License:Apache License
public double[] getData(Interval interval, ConsolidateType consolidateType) throws Exception { DateTime start = interval.getStart().withDayOfMonth(1).withMillisOfDay(0); DateTime end = interval.getEnd();/*from w w w . ja v a2 s .c o m*/ int num = interval.toPeriod(PeriodType.hours()).getHours(); if (interval.getStart().plusHours(num).isBefore(interval.getEnd())) num++; double[] hourly = new double[num]; List<Double> monthly = Lists.newArrayList(); do { double total = 0; int resultIndex = interval.getStart().isBefore(start) ? Hours.hoursBetween(interval.getStart(), start).getHours() : 0; int fromIndex = interval.getStart().isBefore(start) ? 0 : Hours.hoursBetween(start, interval.getStart()).getHours(); double[] data = this.data.get(start); while (resultIndex < num && fromIndex < data.length) { total += data[fromIndex]; hourly[resultIndex++] = data[fromIndex++]; } start = start.plusMonths(1); monthly.add(total); } while (start.isBefore(end)); int hoursInPeriod = (int) (consolidateType.millis / AwsUtils.hourMillis); num = consolidateType == ConsolidateType.monthly ? monthly.size() : (int) Math.ceil(1.0 * num / hoursInPeriod); double[] result = new double[num]; if (consolidateType == ConsolidateType.monthly) { for (int i = 0; i < num; i++) result[i] = monthly.get(i); } else { for (int i = 0; i < hourly.length; i++) result[i / hoursInPeriod] += hourly[i]; } return result; }
From source file:com.netflix.ice.reader.ReaderConfig.java
License:Apache License
private void readData(Product product, DataManager dataManager, Interval interval, ConsolidateType consolidateType) { if (consolidateType == ConsolidateType.hourly) { DateTime start = interval.getStart().withDayOfMonth(1).withMillisOfDay(0); do {//from ww w. j a v a2s. com int hours = dataManager.getDataLength(start); logger.info("found " + hours + " hours data for " + product + " " + interval); start = start.plusMonths(1); } while (start.isBefore(interval.getEnd())); } else if (consolidateType == ConsolidateType.daily) { DateTime start = interval.getStart().withDayOfYear(1).withMillisOfDay(0); do { dataManager.getDataLength(start); start = start.plusYears(1); } while (start.isBefore(interval.getEnd())); } else { dataManager.getData(interval, new TagLists(), TagType.Account, AggregateType.both, false); } }