You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "strongduanmu (via GitHub)" <gi...@apache.org> on 2023/03/15 01:06:47 UTC

[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #24594: Add transaction hook for global clock(#24519)

strongduanmu commented on code in PR #24594:
URL: https://github.com/apache/shardingsphere/pull/24594#discussion_r1136419297


##########
kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/executor/GlobalClockTransactionHook.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.shardingsphere.globalclock.core.executor;
+
+import org.apache.shardingsphere.globalclock.core.provider.GlobalClockProvider;
+import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.infra.lock.LockContext;
+import org.apache.shardingsphere.infra.lock.LockDefinition;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.lock.GlobalLockDefinition;
+import org.apache.shardingsphere.sql.parser.sql.common.enums.TransactionIsolationLevel;
+import org.apache.shardingsphere.transaction.spi.TransactionHookAdapter;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Properties;
+
+/**
+ * Global clock transaction hook.
+ */
+public class GlobalClockTransactionHook extends TransactionHookAdapter {
+    
+    private GlobalClockProvider globalClockProvider;
+    
+    private GlobalClockTransactionExecutor globalClockTransactionExecutor;
+    
+    private LockDefinition lockDefinition = new GlobalLockDefinition("global_clock");
+    
+    private boolean enabled;
+    
+    @Override
+    public void init(final Properties props) {
+        enabled = Boolean.parseBoolean(props.getProperty("enabled"));
+        if (enabled) {
+            globalClockProvider = TypedSPILoader.getService(GlobalClockProvider.class, String.join(".", props.getProperty("type"), props.getProperty("provider")));
+            globalClockTransactionExecutor = TypedSPILoader.getService(GlobalClockTransactionExecutor.class, props.getProperty("trunkType"));
+        }
+    }
+    
+    @Override
+    public void afterBegin(final TransactionConnectionContext transactionContext) {
+        if (!enabled) {
+            return;
+        }
+        transactionContext.setGlobalTimestamp(globalClockProvider.getCurrentTimestamp());
+    }
+    
+    @Override
+    public void afterCreateConnections(final Collection<Connection> connections, final TransactionConnectionContext transactionContext) throws SQLException {
+        if (!enabled) {
+            return;
+        }
+        globalClockTransactionExecutor.sendSnapshotTimestamp(connections, transactionContext.getGlobalTimestamp());
+    }
+    
+    @Override
+    public void beforeExecuteSQL(final Collection<Connection> connections, final TransactionConnectionContext connectionContext, final TransactionIsolationLevel isolationLevel) throws SQLException {
+        if (!enabled) {
+            return;
+        }
+        if (TransactionIsolationLevel.READ_COMMITTED.equals(isolationLevel)) {
+            globalClockTransactionExecutor.sendSnapshotTimestamp(connections, globalClockProvider.getCurrentTimestamp());
+        }
+    }
+    
+    @Override
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public void beforeCommit(final Collection<Connection> connections, final TransactionConnectionContext transactionContext, final LockContext lockContext) throws SQLException {
+        if (!enabled) {
+            return;
+        }
+        if (lockContext.tryLock(lockDefinition, 200)) {
+            globalClockTransactionExecutor.sendCommitTimestamp(connections, globalClockProvider.getCurrentTimestamp());
+        }
+    }
+    
+    @Override
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public void afterCommit(final Collection<Connection> connections, final TransactionConnectionContext transactionContext, final LockContext lockContext) {
+        if (!enabled) {
+            return;
+        }
+        try {
+            globalClockProvider.getNextTimestamp();
+        } finally {
+            lockContext.unlock(lockDefinition);
+        }
+    }
+    
+    @Override
+    public String getType() {
+        return "GlobalClock";

Review Comment:
   Please use uppercase here.



##########
kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/rule/GlobalClockRule.java:
##########
@@ -40,12 +46,26 @@ public final class GlobalClockRule implements GlobalRule {
     
     private final boolean enabled;
     
-    public GlobalClockRule(final GlobalClockRuleConfiguration ruleConfig) {
+    public GlobalClockRule(final GlobalClockRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases) {
         configuration = ruleConfig;
         enabled = ruleConfig.isEnabled();
         globalClockProvider = enabled ? TypedSPILoader.getService(GlobalClockProvider.class, String.join(".", ruleConfig.getType(), ruleConfig.getProvider()),
                 null == ruleConfig.getProps() ? new Properties() : ruleConfig.getProps()) : null;
-        Optional.ofNullable(globalClockProvider).ifPresent(optional -> optional.init(ruleConfig.getProps()));
+        TypedSPILoader.getService(TransactionHook.class, "GlobalClock", getProps(ruleConfig, databases));
+    }
+    
+    private Properties getProps(final GlobalClockRuleConfiguration ruleConfig, final Map<String, ShardingSphereDatabase> databases) {
+        Properties result = new Properties();

Review Comment:
   You can use PropertiesBuilder to reduce code.



-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org