joerghoh commented on code in PR #2982: URL: https://github.com/apache/jackrabbit-oak/pull/2982#discussion_r3497048316
########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/v12/AzureBlobContainerProviderV12.java: ########## @@ -0,0 +1,329 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage.v12; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; +import com.azure.identity.ClientSecretCredential; +import com.azure.identity.ClientSecretCredentialBuilder; +import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.BlobContainerClientBuilder; +import com.azure.storage.blob.BlobServiceClient; +import com.azure.storage.blob.BlobServiceClientBuilder; +import com.azure.storage.blob.models.UserDelegationKey; +import com.azure.storage.blob.sas.BlobSasPermission; +import com.azure.storage.blob.sas.BlobServiceSasSignatureValues; +import com.azure.storage.blob.specialized.BlockBlobClient; +import com.azure.storage.common.policy.RequestRetryOptions; +import org.apache.commons.lang3.StringUtils; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URISyntaxException; +import java.security.InvalidKeyException; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.Properties; + +class AzureBlobContainerProviderV12 { + private static final Logger log = LoggerFactory.getLogger(AzureBlobContainerProviderV12.class); + private static final String DEFAULT_ENDPOINT_SUFFIX = "core.windows.net"; + private final String azureConnectionString; + private final String accountName; + private final String containerName; + private final String blobEndpoint; + private final String sasToken; + private final String accountKey; + private final String tenantId; + private final String clientId; + private final String clientSecret; + // Cached credential — token cache is per-instance, recreating on every SAS call would + // force a new OAuth round-trip each time. + private final ClientSecretCredential clientSecretCredential; + // Cached service client for user-delegation SAS generation — avoids allocating a new Netty + // event loop and connection pool on every SAS call. + private volatile BlobServiceClient cachedBlobServiceClient; + + private AzureBlobContainerProviderV12(Builder builder) { + this.azureConnectionString = builder.azureConnectionString; + this.accountName = builder.accountName; + this.containerName = builder.containerName; + this.blobEndpoint = builder.blobEndpoint; + this.sasToken = builder.sasToken; + this.accountKey = builder.accountKey; + this.tenantId = builder.tenantId; + this.clientId = builder.clientId; + this.clientSecret = builder.clientSecret; + this.clientSecretCredential = StringUtils.isNoneBlank(builder.clientId, builder.clientSecret, builder.tenantId) + ? new ClientSecretCredentialBuilder() + .clientId(builder.clientId) + .clientSecret(builder.clientSecret) + .tenantId(builder.tenantId) + .build() + : null; + } + + /** + * Constructs the Azure Storage endpoint URL. + * If a custom blobEndpoint is configured, it will be used. + * Otherwise, constructs the default endpoint using the account name. + * + * @param accountName the storage account name + * @param customBlobEndpoint optional custom blob endpoint (can be null or empty) + * @return the endpoint URL to use + */ + @NotNull + private static String getEndpointUrl(String accountName, String customBlobEndpoint) { + if (StringUtils.isNotBlank(customBlobEndpoint)) { + // Use custom endpoint (e.g., for private endpoints) + // Ensure it starts with https:// if not already present + if (!customBlobEndpoint.startsWith("http://") && !customBlobEndpoint.startsWith("https://")) { Review Comment: Wouldn't this allow also a http (non-encypted) url to be created? ########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/v12/UtilsV12.java: ########## @@ -0,0 +1,191 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage.v12; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.ProxyOptions; +import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; +import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.BlobServiceClient; +import com.azure.storage.blob.BlobServiceClientBuilder; +import com.azure.storage.common.policy.RequestRetryOptions; +import com.azure.storage.common.policy.RetryPolicyType; +import org.apache.commons.lang3.StringUtils; +import org.apache.jackrabbit.oak.commons.PropertiesUtil; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetSocketAddress; +import java.util.Objects; +import java.util.Properties; + +final class UtilsV12 { + public static final String DASH = "-"; + public static final String DEFAULT_CONFIG_FILE = "azurev12.properties"; + + private UtilsV12() { + } + + public static BlobContainerClient getBlobContainer(@NotNull final String connectionString, + @NotNull final String containerName, + @Nullable final RequestRetryOptions retryOptions, + final Properties properties) throws DataStoreException { + try { + AzureHttpRequestLoggingPolicyV12 loggingPolicy = new AzureHttpRequestLoggingPolicyV12(); + + BlobServiceClientBuilder builder = new BlobServiceClientBuilder() + .connectionString(connectionString) + .retryOptions(retryOptions) + .addPolicy(loggingPolicy); + + HttpClient httpClient = new NettyAsyncHttpClientBuilder() Review Comment: What is the overhead of creating this httpClient? Is this httpClient designed to be reused (like the Apache HttpClient?) ########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreWrapper.java: ########## @@ -0,0 +1,301 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage; + +import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.v12.AzureDataStoreV12; +import org.apache.jackrabbit.oak.commons.PropertiesUtil; +import org.apache.jackrabbit.oak.plugins.blob.AbstractSharedCachingDataStore; +import org.apache.jackrabbit.oak.plugins.blob.datastore.AbstractDataStoreService; +import org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.*; +import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier; +import org.apache.jackrabbit.oak.spi.blob.data.DataRecord; +import org.apache.jackrabbit.oak.spi.blob.data.DataStore; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.apache.jackrabbit.oak.stats.StatisticsProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.net.URI; +import java.util.*; + + +/** + * OSGi component that selects between Azure SDK v8 ({@link AzureDataStore}) and v12 + * ({@link AzureDataStoreV12}) at activation time based on configuration, then registers the + * chosen implementation under the legacy v8 PID so consumers bound to that PID keep working. + * + * <p>Replaces the old dual-service architecture (AzureDataStoreService + AzureDataStoreServiceV12 + * + AzureSDKConditionGate) that caused deadlocks during OSGi service swap on FT toggle. + */ +@Component( + name = AzureDataStoreWrapper.NAME, + configurationPid = AzureDataStoreWrapper.NAME, + configurationPolicy = ConfigurationPolicy.REQUIRE +) +public class AzureDataStoreWrapper extends AbstractDataStoreService { + + private static final Logger log = LoggerFactory.getLogger(AzureDataStoreWrapper.class); + + public static final String NAME = "org.apache.jackrabbit.oak.plugins.blob.datastore.AzureDataStore"; Review Comment: Is there any special reason not to use the full qualified class name? (just found the answer to this in a comment below; you should move that comment here.) ########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreWrapper.java: ########## @@ -0,0 +1,301 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage; + +import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.v12.AzureDataStoreV12; +import org.apache.jackrabbit.oak.commons.PropertiesUtil; +import org.apache.jackrabbit.oak.plugins.blob.AbstractSharedCachingDataStore; +import org.apache.jackrabbit.oak.plugins.blob.datastore.AbstractDataStoreService; +import org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.*; +import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier; +import org.apache.jackrabbit.oak.spi.blob.data.DataRecord; +import org.apache.jackrabbit.oak.spi.blob.data.DataStore; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.apache.jackrabbit.oak.stats.StatisticsProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.net.URI; +import java.util.*; + + +/** + * OSGi component that selects between Azure SDK v8 ({@link AzureDataStore}) and v12 + * ({@link AzureDataStoreV12}) at activation time based on configuration, then registers the + * chosen implementation under the legacy v8 PID so consumers bound to that PID keep working. + * + * <p>Replaces the old dual-service architecture (AzureDataStoreService + AzureDataStoreServiceV12 + * + AzureSDKConditionGate) that caused deadlocks during OSGi service swap on FT toggle. + */ +@Component( + name = AzureDataStoreWrapper.NAME, + configurationPid = AzureDataStoreWrapper.NAME, + configurationPolicy = ConfigurationPolicy.REQUIRE +) +public class AzureDataStoreWrapper extends AbstractDataStoreService { + + private static final Logger log = LoggerFactory.getLogger(AzureDataStoreWrapper.class); + + public static final String NAME = "org.apache.jackrabbit.oak.plugins.blob.datastore.AzureDataStore"; + + // Same name for now; kept as separate constants so they can diverge if the sources need different keys later. + static final String ENV_VAR_V12_ENABLED = "blobstoreAzureV12Enabled"; + static final String OSGI_CONFIG_V12_ENABLED = "blobstoreAzureV12Enabled"; + static final String JVM_PROPERTY_V12_ENABLED = "blob.azure.v12.enabled"; + // Package-private so DelegatingDataStore (inner class) and same-package tests can reach it without reflection. + AbstractSharedCachingDataStore activeImpl; + @Reference + private StatisticsProvider statisticsProvider; + private ServiceRegistration<?> delegateReg; + + static ServiceRegistration<?> registerService(ComponentContext context, AbstractSharedCachingDataStore service) { Review Comment: The method name is a bit too generic. ```suggestion static ServiceRegistration<?> registerDataStoreService(ComponentContext context, AbstractSharedCachingDataStore service) { ``` ########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreWrapper.java: ########## @@ -0,0 +1,301 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage; + +import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.v12.AzureDataStoreV12; +import org.apache.jackrabbit.oak.commons.PropertiesUtil; +import org.apache.jackrabbit.oak.plugins.blob.AbstractSharedCachingDataStore; +import org.apache.jackrabbit.oak.plugins.blob.datastore.AbstractDataStoreService; +import org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.*; +import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier; +import org.apache.jackrabbit.oak.spi.blob.data.DataRecord; +import org.apache.jackrabbit.oak.spi.blob.data.DataStore; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.apache.jackrabbit.oak.stats.StatisticsProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.net.URI; +import java.util.*; + + +/** + * OSGi component that selects between Azure SDK v8 ({@link AzureDataStore}) and v12 + * ({@link AzureDataStoreV12}) at activation time based on configuration, then registers the + * chosen implementation under the legacy v8 PID so consumers bound to that PID keep working. + * + * <p>Replaces the old dual-service architecture (AzureDataStoreService + AzureDataStoreServiceV12 + * + AzureSDKConditionGate) that caused deadlocks during OSGi service swap on FT toggle. + */ +@Component( + name = AzureDataStoreWrapper.NAME, + configurationPid = AzureDataStoreWrapper.NAME, + configurationPolicy = ConfigurationPolicy.REQUIRE +) +public class AzureDataStoreWrapper extends AbstractDataStoreService { + + private static final Logger log = LoggerFactory.getLogger(AzureDataStoreWrapper.class); + + public static final String NAME = "org.apache.jackrabbit.oak.plugins.blob.datastore.AzureDataStore"; + + // Same name for now; kept as separate constants so they can diverge if the sources need different keys later. + static final String ENV_VAR_V12_ENABLED = "blobstoreAzureV12Enabled"; + static final String OSGI_CONFIG_V12_ENABLED = "blobstoreAzureV12Enabled"; + static final String JVM_PROPERTY_V12_ENABLED = "blob.azure.v12.enabled"; + // Package-private so DelegatingDataStore (inner class) and same-package tests can reach it without reflection. + AbstractSharedCachingDataStore activeImpl; + @Reference + private StatisticsProvider statisticsProvider; + private ServiceRegistration<?> delegateReg; + + static ServiceRegistration<?> registerService(ComponentContext context, AbstractSharedCachingDataStore service) { + Dictionary<String, Object> delegateProps = new Hashtable<>(); + // Use the v8 PID so consumers bound to "org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureDataStore" + // still receive this service without needing a config change. + delegateProps.put(Constants.SERVICE_PID, AzureDataStore.class.getName()); + delegateProps.put("oak.datastore.description", new String[]{"type=AzureBlob"}); + return context.getBundleContext().registerService( + AbstractSharedCachingDataStore.class.getName(), service, delegateProps); + } + + /** + * Priority: JVM property (test/local override) > env var (fleet-wide container config) > OSGi config (normal production path). + * Higher-authority sources win so operators can override without touching OSGi config. + */ + static boolean getUseV12Value(Map<String, Object> config) { + if (System.getProperty(JVM_PROPERTY_V12_ENABLED) != null) { + boolean useV12 = Boolean.getBoolean(JVM_PROPERTY_V12_ENABLED); + log.info("Azure SDK v12 flag: JVM property {}={}", JVM_PROPERTY_V12_ENABLED, useV12); + return useV12; + } + String envVar = System.getenv(ENV_VAR_V12_ENABLED); + if (envVar != null) { + boolean useV12 = Boolean.parseBoolean(envVar); + log.info("Azure SDK v12 flag: environment variable {}={}", ENV_VAR_V12_ENABLED, useV12); + return useV12; + } + if (config.containsKey(OSGI_CONFIG_V12_ENABLED)) { + boolean useV12 = PropertiesUtil.toBoolean(config.get(OSGI_CONFIG_V12_ENABLED), false); + log.info("Azure SDK v12 flag: OSGi config {}={}", OSGI_CONFIG_V12_ENABLED, useV12); + return useV12; + } + log.info("Azure SDK v12 flag: not configured, using default (false)"); Review Comment: ```suggestion log.info("Azure SDK v12 flag: not configured, falling back to v8 "); ``` ########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/v12/UtilsV12.java: ########## @@ -0,0 +1,191 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage.v12; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.ProxyOptions; +import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; +import com.azure.storage.blob.BlobContainerClient; +import com.azure.storage.blob.BlobServiceClient; +import com.azure.storage.blob.BlobServiceClientBuilder; +import com.azure.storage.common.policy.RequestRetryOptions; +import com.azure.storage.common.policy.RetryPolicyType; +import org.apache.commons.lang3.StringUtils; +import org.apache.jackrabbit.oak.commons.PropertiesUtil; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetSocketAddress; +import java.util.Objects; +import java.util.Properties; + +final class UtilsV12 { + public static final String DASH = "-"; + public static final String DEFAULT_CONFIG_FILE = "azurev12.properties"; + + private UtilsV12() { + } + + public static BlobContainerClient getBlobContainer(@NotNull final String connectionString, + @NotNull final String containerName, + @Nullable final RequestRetryOptions retryOptions, + final Properties properties) throws DataStoreException { + try { + AzureHttpRequestLoggingPolicyV12 loggingPolicy = new AzureHttpRequestLoggingPolicyV12(); + + BlobServiceClientBuilder builder = new BlobServiceClientBuilder() + .connectionString(connectionString) + .retryOptions(retryOptions) + .addPolicy(loggingPolicy); + + HttpClient httpClient = new NettyAsyncHttpClientBuilder() + .proxy(computeProxyOptions(properties)) + .build(); + + builder.httpClient(httpClient); + + BlobServiceClient blobServiceClient = builder.buildClient(); + return blobServiceClient.getBlobContainerClient(containerName); + + } catch (Exception e) { + throw new DataStoreException(e); + } + } + + public static ProxyOptions computeProxyOptions(final Properties properties) { + String proxyHost = properties.getProperty(AzureConstantsV12.PROXY_HOST); + String proxyPort = properties.getProperty(AzureConstantsV12.PROXY_PORT); + + if (!(Objects.toString(proxyHost, "").isEmpty() || Objects.toString(proxyPort, "").isEmpty())) { Review Comment: I think that the logic is wrong; it should be AND instead of OR. ########## oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreWrapper.java: ########## @@ -0,0 +1,301 @@ +/* + * 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.jackrabbit.oak.blob.cloud.azure.blobstorage; + +import org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.v12.AzureDataStoreV12; +import org.apache.jackrabbit.oak.commons.PropertiesUtil; +import org.apache.jackrabbit.oak.plugins.blob.AbstractSharedCachingDataStore; +import org.apache.jackrabbit.oak.plugins.blob.datastore.AbstractDataStoreService; +import org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.*; +import org.apache.jackrabbit.oak.spi.blob.data.DataIdentifier; +import org.apache.jackrabbit.oak.spi.blob.data.DataRecord; +import org.apache.jackrabbit.oak.spi.blob.data.DataStore; +import org.apache.jackrabbit.oak.spi.blob.data.DataStoreException; +import org.apache.jackrabbit.oak.stats.StatisticsProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.net.URI; +import java.util.*; + + +/** + * OSGi component that selects between Azure SDK v8 ({@link AzureDataStore}) and v12 + * ({@link AzureDataStoreV12}) at activation time based on configuration, then registers the + * chosen implementation under the legacy v8 PID so consumers bound to that PID keep working. + * + * <p>Replaces the old dual-service architecture (AzureDataStoreService + AzureDataStoreServiceV12 + * + AzureSDKConditionGate) that caused deadlocks during OSGi service swap on FT toggle. + */ +@Component( + name = AzureDataStoreWrapper.NAME, + configurationPid = AzureDataStoreWrapper.NAME, + configurationPolicy = ConfigurationPolicy.REQUIRE +) +public class AzureDataStoreWrapper extends AbstractDataStoreService { + + private static final Logger log = LoggerFactory.getLogger(AzureDataStoreWrapper.class); + + public static final String NAME = "org.apache.jackrabbit.oak.plugins.blob.datastore.AzureDataStore"; + + // Same name for now; kept as separate constants so they can diverge if the sources need different keys later. + static final String ENV_VAR_V12_ENABLED = "blobstoreAzureV12Enabled"; + static final String OSGI_CONFIG_V12_ENABLED = "blobstoreAzureV12Enabled"; + static final String JVM_PROPERTY_V12_ENABLED = "blob.azure.v12.enabled"; + // Package-private so DelegatingDataStore (inner class) and same-package tests can reach it without reflection. + AbstractSharedCachingDataStore activeImpl; + @Reference + private StatisticsProvider statisticsProvider; + private ServiceRegistration<?> delegateReg; + + static ServiceRegistration<?> registerService(ComponentContext context, AbstractSharedCachingDataStore service) { + Dictionary<String, Object> delegateProps = new Hashtable<>(); + // Use the v8 PID so consumers bound to "org.apache.jackrabbit.oak.blob.cloud.azure.blobstorage.AzureDataStore" + // still receive this service without needing a config change. + delegateProps.put(Constants.SERVICE_PID, AzureDataStore.class.getName()); + delegateProps.put("oak.datastore.description", new String[]{"type=AzureBlob"}); + return context.getBundleContext().registerService( + AbstractSharedCachingDataStore.class.getName(), service, delegateProps); + } + + /** + * Priority: JVM property (test/local override) > env var (fleet-wide container config) > OSGi config (normal production path). + * Higher-authority sources win so operators can override without touching OSGi config. + */ + static boolean getUseV12Value(Map<String, Object> config) { + if (System.getProperty(JVM_PROPERTY_V12_ENABLED) != null) { + boolean useV12 = Boolean.getBoolean(JVM_PROPERTY_V12_ENABLED); + log.info("Azure SDK v12 flag: JVM property {}={}", JVM_PROPERTY_V12_ENABLED, useV12); + return useV12; + } + String envVar = System.getenv(ENV_VAR_V12_ENABLED); + if (envVar != null) { + boolean useV12 = Boolean.parseBoolean(envVar); + log.info("Azure SDK v12 flag: environment variable {}={}", ENV_VAR_V12_ENABLED, useV12); + return useV12; + } + if (config.containsKey(OSGI_CONFIG_V12_ENABLED)) { + boolean useV12 = PropertiesUtil.toBoolean(config.get(OSGI_CONFIG_V12_ENABLED), false); + log.info("Azure SDK v12 flag: OSGi config {}={}", OSGI_CONFIG_V12_ENABLED, useV12); + return useV12; + } + log.info("Azure SDK v12 flag: not configured, using default (false)"); + return false; + } + + static AbstractSharedCachingDataStore createV8Store(Properties props) { + AzureDataStore v8 = new AzureDataStore(); + v8.setProperties(props); + return v8; + } + + static AbstractSharedCachingDataStore createV12Store(Properties props) { + AzureDataStoreV12 v12 = new AzureDataStoreV12(); + v12.setProperties(props); + return v12; + } + + private static Properties toProperties(Map<String, Object> config) { + Properties p = new Properties(); + p.putAll(config); + return p; + } + + // -- Helpers --------------------------------------------------------- + + @Override + protected DataStore createDataStore(ComponentContext context, Map<String, Object> config) { + boolean useV12 = getUseV12Value(config); + if (useV12) { + log.info("Starting blob store using Azure SDK v12"); + activeImpl = createV12Store(toProperties(config)); + } else { + log.info("Starting blob store using Azure SDK v8"); + activeImpl = createV8Store(toProperties(config)); + } + activeImpl.setStatisticsProvider(getStatisticsProvider()); + // Registers activeImpl separately as AbstractSharedCachingDataStore so consumers + // bound to that type (e.g. oak-repository-service) get the concrete store directly, + // not just the DataStore view the base class exposes. + delegateReg = registerService(context, activeImpl); + + return new DelegatingDataStore(); + } + + @Override + @Deactivate + protected void deactivate() throws DataStoreException { + if (delegateReg != null) { + // Must unregister before super.deactivate() closes the store; otherwise a + // consumer that unbinds late could receive an already-closed DataStore. + delegateReg.unregister(); + delegateReg = null; + } + super.deactivate(); + } + + @Override + protected @NotNull StatisticsProvider getStatisticsProvider() { + return statisticsProvider; + } + + @Override + protected void setStatisticsProvider(StatisticsProvider statisticsProvider) { + this.statisticsProvider = statisticsProvider; + } + + @Override + protected String[] getDescription() { + return new String[]{"type=AzureBlob"}; Review Comment: This is used in the context of the mbeans, is it visible there as well, what SDK version is active? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
