S3Guard { public static PathMetadata getWithTtl(MetadataStore ms, Path path, @Nullable ITtlTimeProvider timeProvider, final boolean needEmptyDirectoryFlag, final boolean allowAuthoritative) throws IOException { final PathMetadata pathMetadata = ms.get(path, needEmptyDirectoryFlag); if (timeProvider == null) { LOG.debug("timeProvider is null, returning pathMetadata as is"); return pathMetadata; } if (allowAuthoritative) { LOG.debug("allowAuthoritative is true, returning pathMetadata as is"); return pathMetadata; } long ttl = timeProvider.getMetadataTtl(); if (pathMetadata != null) { if (pathMetadata.getLastUpdated() == 0) { LOG.debug("PathMetadata TTL for {} is 0, so it will be returned as " + "not expired.", path); return pathMetadata; } if (!pathMetadata.isExpired(ttl, timeProvider.getNow())) { return pathMetadata; } else { LOG.debug("PathMetadata TTl for {} is expired in metadata store" + " -removing entry", path); ms.forgetMetadata(path); return null; } } return null; } private  S3Guard(); @Retries.OnceTranslated static MetadataStore getMetadataStore(FileSystem fs,
      ITtlTimeProvider ttlTimeProvider); @RetryTranslated static boolean refreshEntry(
      MetadataStore metadataStore,
      PathMetadata pm,
      S3AFileStatus s3AFileStatus,
      ITtlTimeProvider timeProvider); @RetryTranslated static S3AFileStatus putAndReturn(MetadataStore ms,
      S3AFileStatus status,
      ITtlTimeProvider timeProvider); @RetryTranslated static S3AFileStatus putAndReturn(
      final MetadataStore ms,
      final S3AFileStatus status,
      final ITtlTimeProvider timeProvider,
      @Nullable final BulkOperationState operationState); @RetryTranslated static void putAuthDirectoryMarker(
      final MetadataStore ms,
      final S3AFileStatus status,
      final ITtlTimeProvider timeProvider,
      @Nullable final BulkOperationState operationState); static BulkOperationState initiateBulkWrite(
      @Nullable final MetadataStore metastore,
      final BulkOperationState.OperationType operation,
      final Path path); static S3AFileStatus[] dirMetaToStatuses(DirListingMetadata dirMeta); static RemoteIterator<S3AFileStatus> dirListingUnion(
          MetadataStore ms, Path path,
          RemoteIterator<S3AFileStatus> backingStatuses,
          DirListingMetadata dirMeta, boolean isAuthoritative,
          ITtlTimeProvider timeProvider,
          Function<S3AFileStatus[], RemoteIterator<S3AFileStatus>> toStatusItr); static boolean isNullMetadataStore(MetadataStore ms); @Deprecated @Retries.OnceExceptionsSwallowed static void makeDirsOrdered(MetadataStore ms, List<Path> dirs,
      String owner, boolean authoritative, ITtlTimeProvider timeProvider); static void addMoveDir(MetadataStore ms, Collection<Path> srcPaths,
      Collection<PathMetadata> dstMetas, Path srcPath, Path dstPath,
      String owner); static void addMoveFile(MetadataStore ms, Collection<Path> srcPaths,
      Collection<PathMetadata> dstMetas, Path srcPath, Path dstPath,
      long size, long blockSize, String owner, String eTag, String versionId); static void addMoveAncestors(MetadataStore ms,
      Collection<Path> srcPaths, Collection<PathMetadata> dstMetas,
      Path srcRoot, Path srcPath, Path dstPath, String owner); @Retries.RetryTranslated static void addAncestors(
      final MetadataStore metadataStore,
      final Path qualifiedPath,
      final ITtlTimeProvider timeProvider,
      @Nullable final BulkOperationState operationState); static void assertQualified(Path p); static void assertQualified(Path...paths); static void putWithTtl(MetadataStore ms,
      DirListingMetadata dirMeta,
      final List<Path> unchangedEntries,
      final ITtlTimeProvider timeProvider,
      @Nullable final BulkOperationState operationState); static void putWithTtl(MetadataStore ms, PathMetadata fileMeta,
      @Nullable ITtlTimeProvider timeProvider,
      @Nullable final BulkOperationState operationState); static void putWithTtl(MetadataStore ms,
      Collection<? extends PathMetadata> fileMetas,
      @Nullable ITtlTimeProvider timeProvider,
      @Nullable final BulkOperationState operationState); static PathMetadata getWithTtl(MetadataStore ms, Path path,
      @Nullable ITtlTimeProvider timeProvider,
      final boolean needEmptyDirectoryFlag,
      final boolean allowAuthoritative); @RetryTranslated static DirListingMetadata listChildrenWithTtl(MetadataStore ms,
      Path path,
      @Nullable ITtlTimeProvider timeProvider,
      boolean allowAuthoritative); static Collection<String> getAuthoritativePaths(S3AFileSystem fs); static boolean allowAuthoritative(Path p, S3AFileSystem fs,
      boolean authMetadataStore, Collection<String> authPaths); static void logS3GuardDisabled(Logger logger, String warnLevelStr,
      String bucket); @InterfaceAudience.Private
  @InterfaceStability.Unstable
  @VisibleForTesting
  static final String S3GUARD_DDB_CLIENT_FACTORY_IMPL; @VisibleForTesting
  static final boolean DIR_MERGE_UPDATES_ALL_RECORDS_NONAUTH; @VisibleForTesting
  static final String DISABLED_LOG_MSG; static final String UNKNOWN_WARN_LEVEL; }
@Test public void testGetWithTtlExpired() throws Exception { S3AFileStatus fileStatus = mock(S3AFileStatus.class); Path path = new Path("/file"); when(fileStatus.getPath()).thenReturn(path); PathMetadata pm = new PathMetadata(fileStatus); pm.setLastUpdated(100L); MetadataStore ms = mock(MetadataStore.class); when(ms.get(path, false)).thenReturn(pm); ITtlTimeProvider timeProvider = mock(ITtlTimeProvider.class); when(timeProvider.getNow()).thenReturn(101L); when(timeProvider.getMetadataTtl()).thenReturn(1L); final PathMetadata pmExpired = S3Guard.getWithTtl(ms, path, timeProvider, false, false); assertNull(pmExpired); }
@Test public void testGetWithTtlNotExpired() throws Exception { S3AFileStatus fileStatus = mock(S3AFileStatus.class); Path path = new Path("/file"); when(fileStatus.getPath()).thenReturn(path); PathMetadata pm = new PathMetadata(fileStatus); pm.setLastUpdated(100L); MetadataStore ms = mock(MetadataStore.class); when(ms.get(path, false)).thenReturn(pm); ITtlTimeProvider timeProvider = mock(ITtlTimeProvider.class); when(timeProvider.getNow()).thenReturn(101L); when(timeProvider.getMetadataTtl()).thenReturn(2L); final PathMetadata pmNotExpired = S3Guard.getWithTtl(ms, path, timeProvider, false, false); assertNotNull(pmNotExpired); }
@Test public void testGetWithZeroLastUpdatedNotExpired() throws Exception { S3AFileStatus fileStatus = mock(S3AFileStatus.class); Path path = new Path("/file"); when(fileStatus.getPath()).thenReturn(path); PathMetadata pm = new PathMetadata(fileStatus); pm.setLastUpdated(0L); MetadataStore ms = mock(MetadataStore.class); when(ms.get(path, false)).thenReturn(pm); ITtlTimeProvider timeProvider = mock(ITtlTimeProvider.class); when(timeProvider.getNow()).thenReturn(101L); when(timeProvider.getMetadataTtl()).thenReturn(2L); final PathMetadata pmExpired = S3Guard.getWithTtl(ms, path, timeProvider, false, false); assertNotNull(pmExpired); }