Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(datastore): compatible with ali oss #1132

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,25 @@ public class WalManager extends Thread {

private int logFileIndex;

private final int ossMaxAttempts;

private final List<String> existedLogFiles = new ArrayList<>();

public WalManager(ObjectStore objectStore,
SwBufferManager bufferManager,
@Value("${sw.datastore.walFileSize}") int walFileSize,
@Value("${sw.datastore.walMaxFileSize}") int walMaxFileSize,
@Value("${sw.datastore.walPrefix}") String walPrefix,
@Value("${sw.datastore.walWaitIntervalMillis}") int walWaitIntervalMillis) throws IOException {
@Value("${sw.datastore.walWaitIntervalMillis}") int walWaitIntervalMillis,
@Value("${sw.datastore.ossMaxAttempts}") int ossMaxAttempts) throws IOException {
this.objectStore = objectStore;
this.bufferManager = bufferManager;
this.walFileSize = walFileSize;
this.walMaxFileSize = walMaxFileSize;
this.walMaxFileSizeNoHeader = this.walMaxFileSize - this.header.length;
this.logFilePrefix = walPrefix + "wal.log.";
this.walWaitIntervalMillis = walWaitIntervalMillis;
this.ossMaxAttempts = ossMaxAttempts;
this.outputBuffer = this.bufferManager.allocate(this.walMaxFileSizeNoHeader);
this.compressedBuffer = this.bufferManager.allocate(this.walMaxFileSize);
this.outputStream = new SwBufferOutputStream(this.outputBuffer);
Expand All @@ -96,7 +100,7 @@ public WalManager(ObjectStore objectStore,
try {
it = Retry.decorateCheckedSupplier(
Retry.of("put", RetryConfig.custom()
.maxAttempts(10000)
.maxAttempts(ossMaxAttempts)
.intervalFunction(IntervalFunction.ofExponentialRandomBackoff(100, 2.0, 0.5, 10000))
.retryOnException(e -> !terminated)
.build()),
Expand Down Expand Up @@ -162,7 +166,7 @@ private void getNext() {
try {
data = Retry.decorateCheckedSupplier(
Retry.of("get", RetryConfig.custom()
.maxAttempts(10000)
.maxAttempts(WalManager.this.ossMaxAttempts)
.intervalFunction(IntervalFunction.ofExponentialRandomBackoff(100, 2.0, 0.5, 10000))
.build()),
() -> objectStore.get(fn))
Expand Down Expand Up @@ -342,7 +346,7 @@ private void writeToObjectStore(boolean clearOutput) {
int compressedBufferSize = compressedSize + this.header.length;
Retry.decorateCheckedRunnable(
Retry.of("put", RetryConfig.custom()
.maxAttempts(10000)
.maxAttempts(this.ossMaxAttempts)
.intervalFunction(IntervalFunction.ofExponentialRandomBackoff(100, 2.0, 0.5, 10000))
.build()),
() -> this.objectStore.put(this.logFilePrefix + this.logFileIndex,
Expand Down
1 change: 1 addition & 0 deletions server/controller/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sw:
walMaxFileSize: ${SW_DATASTORE_WAL_MAX_FILE_SIZE:67108864}
walPrefix: ${SW_DATASTORE_WAL_LOG_PREFIX:wal/}
walWaitIntervalMillis: ${SW_DATASTORE_WAL_WAIT_INTERVAL_MILLIS:500}
ossMaxAttempts: ${SW_DATASTORE_WAL_INIT_MAX_ATTEMPTS:3}
---
#Development
spring:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class DataStoreTest {
public void setUp() throws IOException {
this.bufferManager = new SwByteBufferManager();
this.objectStore = new FileSystemObjectStore(bufferManager, this.rootDir.getAbsolutePath());
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
this.dataStore = new DataStore(this.walManager);
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public void testUpdate() throws IOException {
is(List.of(Map.of("k", "3", "x", "2"))));

this.dataStore.terminate();
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
this.dataStore = new DataStore(this.walManager);
assertThat("t1",
this.dataStore.scan(DataStoreScanRequest.builder()
Expand Down Expand Up @@ -534,7 +534,7 @@ public void testScanMultipleTables() {
@Test
public void testMultiThreads() throws Throwable {
this.dataStore.terminate();
this.walManager = new WalManager(this.objectStore, this.bufferManager, 65536, 65536 * 1024, "test/", 1000);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 65536, 65536 * 1024, "test/", 1000, 3);
this.dataStore = new DataStore(this.walManager);
abstract class TestThread extends Thread {
protected final Random random = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class WalManagerTest {
public void setUp() throws IOException {
this.bufferManager = new SwByteBufferManager();
this.objectStore = new FileSystemObjectStore(this.bufferManager, this.rootDir.getAbsolutePath());
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
}


Expand Down Expand Up @@ -167,7 +167,7 @@ public void testSimple() throws IOException, InterruptedException {
this.walManager.append(entries.get(3));
this.walManager.terminate();
assertThat(ImmutableList.copyOf(this.objectStore.list("")), is(List.of("test/wal.log.0", "test/wal.log.1")));
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
assertThat(ImmutableList.copyOf(this.walManager.readAll()), is(entries));
}

Expand All @@ -190,7 +190,7 @@ public void testMany() throws IOException, InterruptedException {
this.walManager.append(entry);
}
this.walManager.terminate();
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
assertThat(ImmutableList.copyOf(this.walManager.readAll()), is(entries));
}

Expand All @@ -206,7 +206,7 @@ public void testHugeEntry() throws IOException {
.build();
this.walManager.append(entry);
this.walManager.terminate();
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
var entries = ImmutableList.copyOf(this.walManager.readAll());
assertThat(entries.size(), greaterThan(1));
assertThat(entries.get(0).getTableSchema(), is(entry.getTableSchema()));
Expand Down Expand Up @@ -267,11 +267,12 @@ public void testAppendSplitSizeCalculation() throws IOException {
256,
entry1.getSerializedSize() + CodedOutputStream.computeUInt32SizeNoTag(entry1.getSerializedSize()) + 4,
"test/",
10);
10,
3);
builder.addAllRecords(entry2.getRecordsList());
this.walManager.append(builder.build());
this.walManager.terminate();
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(this.objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
assertThat(ImmutableList.copyOf(this.walManager.readAll()), is(List.of(entry1, entry2)));
}

Expand All @@ -283,7 +284,7 @@ public void testWriteFailureAndRetry() throws Exception {
.doThrow(new IOException())
.doNothing()
.when(objectStore).put(anyString(), any());
var walManager = new WalManager(objectStore, this.bufferManager, 256, 4096, "test/", 10);
var walManager = new WalManager(objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
walManager.append(Wal.WalEntry.newBuilder()
.setEntryType(Wal.WalEntry.Type.UPDATE)
.setTableName("t")
Expand All @@ -302,7 +303,7 @@ public void testReadFailureAndRetry() throws Exception {
given(objectStore.get(anyString())).willThrow(new IOException())
.willThrow(new IOException())
.willReturn(this.bufferManager.allocate(10));
var walManager = new WalManager(objectStore, this.bufferManager, 256, 4096, "test/", 10);
var walManager = new WalManager(objectStore, this.bufferManager, 256, 4096, "test/", 10, 3);
//noinspection ResultOfMethodCallIgnored
ImmutableList.copyOf(walManager.readAll());
walManager.terminate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static List<MemoryTable.RecordResult> scanAll(MemoryTable memoryTable,
public void setUp() throws IOException {
SwBufferManager bufferManager = new SwByteBufferManager();
FileSystemObjectStore objectStore = new FileSystemObjectStore(bufferManager, this.rootDir.getAbsolutePath());
this.walManager = new WalManager(objectStore, bufferManager, 256, 4096, "test/", 10);
this.walManager = new WalManager(objectStore, bufferManager, 256, 4096, "test/", 10, 3);
}

@AfterEach
Expand Down Expand Up @@ -441,7 +441,7 @@ public void testUpdateFromWal() throws IOException {
SwBufferManager bufferManager = new SwByteBufferManager();
FileSystemObjectStore objectStore = new FileSystemObjectStore(bufferManager,
MemoryTableImplTest.this.rootDir.getAbsolutePath());
MemoryTableImplTest.this.walManager = new WalManager(objectStore, bufferManager, 256, 4096, "test/", 10);
MemoryTableImplTest.this.walManager = new WalManager(objectStore, bufferManager, 256, 4096, "test/", 10, 3);
this.memoryTable = new MemoryTableImpl("test", MemoryTableImplTest.this.walManager);
var it = MemoryTableImplTest.this.walManager.readAll();
while (it.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@

import ai.starwhale.mlops.storage.StorageAccessService;
import ai.starwhale.mlops.storage.StorageObjectInfo;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.stream.Stream;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
Expand All @@ -50,7 +53,11 @@ public StorageAccessServiceS3(S3Config s3Config) {
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
s3Config.getAccessKey(),
s3Config.getSecretKey());
final S3Configuration config = S3Configuration.builder()
.chunkedEncodingEnabled(false)
.build();
S3ClientBuilder s3ClientBuilder = S3Client.builder()
.serviceConfiguration(config)
.credentialsProvider(StaticCredentialsProvider.create(awsCreds))
.region(Region.of(s3Config.getRegion()));
if (s3Config.overWriteEndPoint()) {
Expand Down Expand Up @@ -126,9 +133,13 @@ public InputStream get(String path, Long offset, Long size) throws IOException {

@Override
public Stream<String> list(String path) {
final ListObjectsResponse listObjectsResponse = s3client.listObjects(
ListObjectsRequest.builder().bucket(s3Config.getBucket()).prefix(path).build());
return listObjectsResponse.contents().stream().map(S3Object::key);
try {
final ListObjectsResponse listObjectsResponse = s3client.listObjects(
ListObjectsRequest.builder().bucket(s3Config.getBucket()).prefix(path).build());
return listObjectsResponse.contents().stream().map(S3Object::key);
} catch (NoSuchKeyException e) {
return Stream.empty();
}
}

@Override
Expand Down