seropian commented on code in PR #2989:
URL: https://github.com/apache/jackrabbit-oak/pull/2989#discussion_r3519574305


##########
oak-blob-cloud-azure/src/main/java/org/apache/jackrabbit/oak/blob/cloud/azure/blobstorage/AzureDataStoreWrapper.java:
##########
@@ -0,0 +1,392 @@
+/*
+ * 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.commons.lang3.StringUtils;
+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.SharedDataStore;
+import 
org.apache.jackrabbit.oak.plugins.blob.datastore.AbstractDataStoreService;
+import org.apache.jackrabbit.oak.plugins.blob.datastore.TypedDataStore;
+import org.apache.jackrabbit.oak.plugins.blob.datastore.directaccess.*;
+import org.apache.jackrabbit.oak.spi.blob.BlobOptions;
+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.spi.blob.data.MultiDataStoreAware;
+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.File;
+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<AbstractSharedCachingDataStore> delegateReg;
+
+    static ServiceRegistration<AbstractSharedCachingDataStore> 
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, 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 (StringUtils.isNotBlank(envVar)) {
+            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,

Review Comment:
   internal client, will remove comment 



-- 
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]

Reply via email to