kezhenxu94 commented on a change in pull request #6232:
URL: https://github.com/apache/skywalking/pull/6232#discussion_r560158257



##########
File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java
##########
@@ -106,12 +103,32 @@ public boolean trySampling(String operationName) {
      * sampled, the trace beginning at local, has less chance to be sampled.
      */
     public void forceSampled() {
-        if (on) {
+        if (samplingRateWatcher.getSamplingRate() > 0) {
             samplingFactorHolder.incrementAndGet();
         }
     }
 
     private void resetSamplingFactor() {
         samplingFactorHolder = new AtomicInteger(0);
     }
+
+    /**
+     * reset ScheduledFuture to support samplingRate changed.
+     */
+    void resetScheduledFuture() {
+        if (scheduledFuture != null) {
+            scheduledFuture.cancel(true);
+        }

Review comment:
       Duplicate with line 63-69
   ```suggestion
   ```

##########
File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/dynamic/ConfigurationDiscoveryService.java
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.skywalking.apm.agent.core.conf.dynamic;
+
+import io.grpc.Channel;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import lombok.Getter;
+import org.apache.skywalking.apm.agent.core.boot.BootService;
+import org.apache.skywalking.apm.agent.core.boot.DefaultImplementor;
+import org.apache.skywalking.apm.agent.core.boot.DefaultNamedThreadFactory;
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.commands.CommandService;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+import org.apache.skywalking.apm.agent.core.remote.GRPCChannelListener;
+import org.apache.skywalking.apm.agent.core.remote.GRPCChannelManager;
+import org.apache.skywalking.apm.agent.core.remote.GRPCChannelStatus;
+import 
org.apache.skywalking.apm.network.agent.dynamic.configuration.v3.ConfigurationDiscoveryServiceGrpc;
+import 
org.apache.skywalking.apm.network.agent.dynamic.configuration.v3.ConfigurationSyncRequest;
+import org.apache.skywalking.apm.network.common.v3.Commands;
+import org.apache.skywalking.apm.network.common.v3.KeyStringValuePair;
+import 
org.apache.skywalking.apm.network.trace.component.command.ConfigurationDiscoveryCommand;
+import org.apache.skywalking.apm.util.RunnableWithExceptionProtection;
+
+import static 
org.apache.skywalking.apm.agent.core.conf.Config.Collector.GRPC_UPSTREAM_TIMEOUT;
+
+@DefaultImplementor
+public class ConfigurationDiscoveryService implements BootService, 
GRPCChannelListener {
+
+    /**
+     * Uuid of the last return value.
+     */
+    private String uuid;
+    private Register register = new Register();
+
+    private volatile ScheduledFuture<?> getDynamicConfigurationFuture;
+    private volatile GRPCChannelStatus status = GRPCChannelStatus.DISCONNECT;
+    private volatile 
ConfigurationDiscoveryServiceGrpc.ConfigurationDiscoveryServiceBlockingStub 
configurationDiscoveryServiceBlockingStub;
+
+    public static final String LINE_SEPARATOR = 
System.getProperty("line.separator", "\n");
+    private static final ILog LOGGER = 
LogManager.getLogger(ConfigurationDiscoveryService.class);
+
+    @Override
+    public void statusChanged(final GRPCChannelStatus status) {
+        if (GRPCChannelStatus.CONNECTED.equals(status)) {
+            Channel channel = 
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).getChannel();
+            configurationDiscoveryServiceBlockingStub = 
ConfigurationDiscoveryServiceGrpc.newBlockingStub(channel);
+        } else {
+            configurationDiscoveryServiceBlockingStub = null;
+        }
+        this.status = status;
+    }
+
+    @Override
+    public void prepare() throws Throwable {
+        
ServiceManager.INSTANCE.findService(GRPCChannelManager.class).addChannelListener(this);
+    }
+
+    @Override
+    public void boot() throws Throwable {
+        getDynamicConfigurationFuture = 
Executors.newSingleThreadScheduledExecutor(
+            new DefaultNamedThreadFactory("ConfigurationDiscoveryService")
+        ).scheduleAtFixedRate(
+            new RunnableWithExceptionProtection(
+                this::getAgentDynamicConfig,
+                t -> LOGGER.error("Sync config from OAP error.", t)
+            ),
+            Config.Collector.GET_AGENT_DYNAMIC_CONFIG_INTERVAL,
+            Config.Collector.GET_AGENT_DYNAMIC_CONFIG_INTERVAL, 
TimeUnit.SECONDS
+        );
+    }
+
+    @Override
+    public void onComplete() throws Throwable {
+
+    }
+
+    @Override
+    public void shutdown() throws Throwable {
+        if (getDynamicConfigurationFuture != null) {
+            getDynamicConfigurationFuture.cancel(true);
+        }
+    }
+
+    /**
+     * Register dynamic configuration watcher.
+     *
+     * @param watcher dynamic configuration watcher
+     */
+    public void registerAgentConfigChangeWatcher(AgentConfigChangeWatcher 
watcher) {
+        WatcherHolder holder = new WatcherHolder(watcher);
+        if (register.containsKey(holder.getKey())) {
+            throw new IllegalStateException("Duplicate register, watcher=" + 
watcher);
+        }
+        register.put(holder.getKey(), holder);
+    }
+
+    /**
+     * Process ConfigurationDiscoveryCommand and notify each configuration 
watcher.
+     *
+     * @param configurationDiscoveryCommand Describe dynamic configuration 
information
+     */
+    public void 
handConfigurationDiscoveryCommand(ConfigurationDiscoveryCommand 
configurationDiscoveryCommand) {

Review comment:
       ```suggestion
       public void 
handleConfigurationDiscoveryCommand(ConfigurationDiscoveryCommand 
configurationDiscoveryCommand) {
   ```

##########
File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/commands/executor/ConfigurationDiscoveryCommandExecutor.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.skywalking.apm.agent.core.commands.executor;
+
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.commands.CommandExecutionException;
+import org.apache.skywalking.apm.agent.core.commands.CommandExecutor;
+import 
org.apache.skywalking.apm.agent.core.conf.dynamic.ConfigurationDiscoveryService;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+import org.apache.skywalking.apm.network.trace.component.command.BaseCommand;
+import 
org.apache.skywalking.apm.network.trace.component.command.ConfigurationDiscoveryCommand;
+
+public class ConfigurationDiscoveryCommandExecutor implements CommandExecutor {
+
+    private static final ILog LOGGER = 
LogManager.getLogger(ConfigurationDiscoveryCommandExecutor.class);
+
+    @Override
+    public void execute(BaseCommand command) throws CommandExecutionException {
+        try {
+            ConfigurationDiscoveryCommand agentDynamicConfigurationCommand = 
(ConfigurationDiscoveryCommand) command;
+
+            
ServiceManager.INSTANCE.findService(ConfigurationDiscoveryService.class)
+                                   
.handConfigurationDiscoveryCommand(agentDynamicConfigurationCommand);

Review comment:
       ```suggestion
                                      
.handleConfigurationDiscoveryCommand(agentDynamicConfigurationCommand);
   ```

##########
File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingRateWatcher.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.skywalking.apm.agent.core.sampling;
+
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import 
org.apache.skywalking.apm.agent.core.conf.dynamic.AgentConfigChangeWatcher;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+
+public class SamplingRateWatcher extends AgentConfigChangeWatcher {
+
+    private AtomicReference<Integer> samplingRateReference;

Review comment:
       Use `AtomicInteger` instead of `AtomicReference<Integer>`, also rename 
the field and make it final




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to