This is an automated email from the ASF dual-hosted git repository. ycai pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/cassandra-sidecar.git
The following commit(s) were added to refs/heads/trunk by this push: new 835b0f7 CASSANDRASC-110 Fix NPE thrown when getting StorageClient from pool (#101) 835b0f7 is described below commit 835b0f72b5e8a6fd5c9ac29b97a47942b97740b2 Author: Yifan Cai <52585731+yifa...@users.noreply.github.com> AuthorDate: Thu Feb 29 08:39:06 2024 -0800 CASSANDRASC-110 Fix NPE thrown when getting StorageClient from pool (#101) patch by Yifan Cai; reviewed by Francisco Guerrero for CASSANDRASC-110 --- CHANGES.txt | 1 + .../sidecar/restore/StorageClientPool.java | 42 +++++++++---------- .../sidecar/restore/StorageClientPoolTest.java | 45 +++++++++++++++++++++ src/test/resources/certs/ca.p12 | Bin 2437 -> 3564 bytes src/test/resources/certs/test.p12 | Bin 3533 -> 3564 bytes 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index e1ac034..b46c455 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ 1.0.0 ----- + * Fix NPE thrown when getting StorageClient from pool (CASSANDRASC-110) * Relocate Sidecar common classes in vertx-client-shaded (CASSANDRASC-104) * Automated yaml type binding for deserialization (CASSANDRASC-103) * Upgrade Vert.x version in Sidecar to 4.5 (CASSANDRASC-101) diff --git a/src/main/java/org/apache/cassandra/sidecar/restore/StorageClientPool.java b/src/main/java/org/apache/cassandra/sidecar/restore/StorageClientPool.java index 55f6c5a..eafe600 100644 --- a/src/main/java/org/apache/cassandra/sidecar/restore/StorageClientPool.java +++ b/src/main/java/org/apache/cassandra/sidecar/restore/StorageClientPool.java @@ -77,7 +77,27 @@ public class StorageClientPool implements SdkAutoCloseable sharedExecutor.allowCoreThreadTimeOut(true); } - public StorageClient storageClient(String region) + public StorageClient storageClient(RestoreJob restoreJob) throws RestoreJobFatalException + { + String region = restoreJob.secrets.readCredentials().region(); + StorageClient client = clientByJobId.computeIfAbsent(restoreJob.jobId, id -> storageClient(region)); + return client.authenticate(restoreJob); + } + + /** + * Revoke the credentials for the restore job that is identified by the id + * + * @param jobId id of the restore job + */ + public void revokeCredentials(UUID jobId) + { + clientByJobId.computeIfPresent(jobId, (id, client) -> { + client.revokeCredentials(id); + return null; + }); + } + + private StorageClient storageClient(String region) { return clientPool.computeIfAbsent(region, k -> { Map<SdkAdvancedAsyncClientOption<?>, ?> advancedOptions = Collections.singletonMap( @@ -111,26 +131,6 @@ public class StorageClientPool implements SdkAutoCloseable }); } - public StorageClient storageClient(RestoreJob restoreJob) throws RestoreJobFatalException - { - StorageClient client = storageClient(restoreJob.secrets.readCredentials().region()); - client = clientByJobId.putIfAbsent(restoreJob.jobId, client); - return client.authenticate(restoreJob); - } - - /** - * Revoke the credentials for the restore job that is identified by the id - * - * @param jobId id of the restore job - */ - public void revokeCredentials(UUID jobId) - { - clientByJobId.computeIfPresent(jobId, (id, client) -> { - client.revokeCredentials(id); - return null; - }); - } - @Override public void close() { diff --git a/src/test/java/org/apache/cassandra/sidecar/restore/StorageClientPoolTest.java b/src/test/java/org/apache/cassandra/sidecar/restore/StorageClientPoolTest.java new file mode 100644 index 0000000..3a31d99 --- /dev/null +++ b/src/test/java/org/apache/cassandra/sidecar/restore/StorageClientPoolTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.sidecar.restore; + +import java.util.UUID; + +import org.junit.jupiter.api.Test; + +import org.apache.cassandra.sidecar.config.yaml.SidecarConfigurationImpl; +import org.apache.cassandra.sidecar.db.RestoreJob; +import org.apache.cassandra.sidecar.exceptions.RestoreJobFatalException; +import org.apache.cassandra.sidecar.foundation.RestoreJobSecretsGen; + +import static org.assertj.core.api.Assertions.assertThat; + +class StorageClientPoolTest +{ + @Test + void testRetrieveClient() throws RestoreJobFatalException + { + StorageClientPool pool = new StorageClientPool(new SidecarConfigurationImpl(), null); + RestoreJob job = RestoreJob.builder() + .jobId(UUID.randomUUID()) + .jobSecrets(RestoreJobSecretsGen.genRestoreJobSecrets()) + .build(); + StorageClient client = pool.storageClient(job); + assertThat(client).isNotNull(); + } +} diff --git a/src/test/resources/certs/ca.p12 b/src/test/resources/certs/ca.p12 index 39a1899..591c7fd 100644 Binary files a/src/test/resources/certs/ca.p12 and b/src/test/resources/certs/ca.p12 differ diff --git a/src/test/resources/certs/test.p12 b/src/test/resources/certs/test.p12 index 3436057..591c7fd 100644 Binary files a/src/test/resources/certs/test.p12 and b/src/test/resources/certs/test.p12 differ --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org