Skip to content

Commit

Permalink
chore: attempting to get some buckets cleanup info
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWhitehead committed Aug 29, 2024
1 parent 1494809 commit 11eb7c7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
16 changes: 8 additions & 8 deletions .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ mvn -version
echo ${JOB_TYPE}

# attempt to install 3 times with exponential backoff (starting with 10 seconds)
retry_with_backoff 3 10 \
mvn install -B -V -ntp \
-DskipTests=true \
-Dclirr.skip=true \
-Denforcer.skip=true \
-Dmaven.javadoc.skip=true \
-Dgcloud.download.skip=true \
-T 1C
#retry_with_backoff 3 10 \
# mvn install -B -V -ntp \
# -DskipTests=true \
# -Dclirr.skip=true \
# -Denforcer.skip=true \
# -Dmaven.javadoc.skip=true \
# -Dgcloud.download.skip=true \
# -T 1C

# if GOOGLE_APPLICATION_CREDENTIALS is specified as a relative path, prepend Kokoro root directory onto it
if [[ ! -z "${GOOGLE_APPLICATION_CREDENTIALS}" && "${GOOGLE_APPLICATION_CREDENTIALS}" != /* ]]; then
Expand Down
8 changes: 8 additions & 0 deletions samples/install-without-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<test>com.example.storage.ITVerboseBucketCleanupTest</test>
</properties>


Expand All @@ -32,6 +33,13 @@
<artifactId>google-cloud-storage</artifactId>
<version>2.42.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.42.0</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage-control</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

<modules>
<module>install-without-bom</module>
<module>snapshot</module>
<module>snippets</module>
<!-- <module>snapshot</module>-->
<!-- <module>snippets</module>-->
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.example.storage;

import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BucketField;
import com.google.cloud.storage.Storage.BucketListOption;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.it.BucketCleaner;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;

public final class ITVerboseBucketCleanupTest {
private static final Logger LOGGER = Logger.getLogger(ITVerboseBucketCleanupTest.class.getName());

@Test
public void verboseBucketCleanup() throws Exception {
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("cp-pool-%03d")
.build();

ListeningExecutorService exec = MoreExecutors.listeningDecorator(
Executors.newFixedThreadPool(4 * Runtime.getRuntime().availableProcessors(), threadFactory)
);

OffsetDateTime now = Instant.now().atOffset(ZoneOffset.UTC);
OffsetDateTime _7DaysAgo = now.minusDays(7);

try (Storage s = StorageOptions.http().build().getService()) {
List<ListenableFuture<Void>> deleteFutures = s.list(
BucketListOption.fields(BucketField.NAME, BucketField.TIME_CREATED))
.streamAll()
.map(bucket -> {
String name = bucket.getName();
OffsetDateTime ctime = bucket.getCreateTimeOffsetDateTime();
LOGGER.warning(String.format("bucket = {name: '%s', ctime: '%s'}%n", name, ctime));
if (ctime.isBefore(_7DaysAgo)) {
return exec.<Void>submit(() -> {
BucketCleaner.doCleanup(name, s);
return null;
});
} else {
return Futures.immediateVoidFuture();
}
})
.collect(Collectors.toList());

ListenableFuture<List<@Nullable Void>> allFuture = Futures.allAsList(deleteFutures);

allFuture.get(15, TimeUnit.MINUTES);
}

}
}

0 comments on commit 11eb7c7

Please sign in to comment.