You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by wu...@apache.org on 2018/05/21 07:47:36 UTC

[incubator-skywalking] branch context-switchable created (now 75cf2e9)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a change to branch context-switchable
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git.


      at 75cf2e9  Provide context switchable mode.

This branch includes the following new commits:

     new 75cf2e9  Provide context switchable mode.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


-- 
To stop receiving notification emails like this one, please contact
wusheng@apache.org.

[incubator-skywalking] 01/01: Provide context switchable mode.

Posted by wu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch context-switchable
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git

commit 75cf2e946b8915de2d9729f0f5f479855dc91b0f
Author: wusheng <wu...@foxmail.com>
AuthorDate: Mon May 21 15:47:26 2018 +0800

    Provide context switchable mode.
---
 .../agent/core/context/AbstractTracerContext.java  | 19 +++++++++
 .../apm/agent/core/context/ContextManager.java     | 36 ++++++++++++++++
 .../apm/agent/core/context/TracingContext.java     | 48 +++++++++++++++++++++-
 .../agent/core/context/savepoint/ISavepoint.java   | 28 +++++++++++++
 .../core/context/savepoint/TracingSavepoint.java   | 43 +++++++++++++++++++
 5 files changed, 172 insertions(+), 2 deletions(-)

diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
index e3ffdac..0fc356c 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
@@ -19,6 +19,7 @@
 
 package org.apache.skywalking.apm.agent.core.context;
 
+import org.apache.skywalking.apm.agent.core.context.savepoint.ISavepoint;
 import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
 
 /**
@@ -104,4 +105,22 @@ public interface AbstractTracerContext {
      * @param span to finish
      */
     void stopSpan(AbstractSpan span);
+
+    /**
+     * Get the current savepoint, if context could give.
+     * In normal, there should be at least one active span in active stack.
+     *
+     * @return
+     */
+    ISavepoint savepoint();
+
+    /**
+     * Rollback to the given savepoint.
+     *
+     * The savepoint must be from this context,
+     * and the savepoint is still available to rollback to, such as the span is still active.
+     *
+     * @param savepoint for rollback to.
+     */
+    void rollback(ISavepoint savepoint);
 }
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
index 61e1e9d..49cb8b5 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
@@ -72,11 +72,47 @@ public class ContextManager implements TracingContextListener, BootService, Igno
         return context;
     }
 
+    /**
+     * Get the current tracer context
+     *
+     * @return current context, or NULL if there is no context.
+     */
     private static AbstractTracerContext get() {
         return CONTEXT.get();
     }
 
     /**
+     * Get the current tracer context, then clear the context in current thread.
+     * This method should not be used when you want to move the context from one thread to another,
+     * and the original thread is not used for this context anymore.
+     *
+     * Consider this is used in certain and particular scenario only.
+     *
+     * @return
+     */
+    private static AbstractTracerContext getAndClear(){
+        AbstractTracerContext context = get();
+        CONTEXT.remove();
+        return context;
+    }
+
+
+    /**
+     * Use the given context, which is usually from other thread, through {@link #get()}
+     * The set op can be done, if and only if the current thread has no context,
+     * the reason for check this, is for avoiding the context and active span stack leak.
+     *
+     * @param existedContext
+     */
+    public static void use(AbstractTracerContext existedContext) {
+        if (get() == null) {
+            CONTEXT.set(existedContext);
+        } else {
+            throw new IllegalStateException("Can't use the existed context override the other one");
+        }
+    }
+
+    /**
      * @return the first global trace id if needEnhance. Otherwise, "N/A".
      */
     public static String getGlobalTraceId() {
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
index 1bff777..091d411 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
@@ -22,6 +22,8 @@ import java.util.LinkedList;
 import java.util.List;
 import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
 import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.context.savepoint.ISavepoint;
+import org.apache.skywalking.apm.agent.core.context.savepoint.TracingSavepoint;
 import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
 import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
 import org.apache.skywalking.apm.agent.core.context.trace.EntrySpan;
@@ -94,7 +96,7 @@ public class TracingContext implements AbstractTracerContext {
      * Inject the context into the given carrier, only when the active span is an exit one.
      *
      * @param carrier to carry the context for crossing process.
-     * @throws IllegalStateException  if the active span isn't an exit one.
+     * @throws IllegalStateException if the active span isn't an exit one.
      * Ref to {@link AbstractTracerContext#inject(ContextCarrier)}
      */
     @Override
@@ -316,7 +318,7 @@ public class TracingContext implements AbstractTracerContext {
      * @param operationName most likely a service name of remote
      * @param remotePeer the network id(ip:port, hostname:port or ip1:port1,ip2,port, etc.)
      * @return the span represent an exit point of this segment.
-     * @see  ExitSpan
+     * @see ExitSpan
      */
     @Override
     public AbstractSpan createExitSpan(final String operationName, final String remotePeer) {
@@ -419,6 +421,48 @@ public class TracingContext implements AbstractTracerContext {
         }
     }
 
+    @Override public ISavepoint savepoint() {
+        if (activeSpanStack.size() > 0) {
+            return new TracingSavepoint(this, peek());
+        } else {
+            throw new IllegalStateException("Can't create savepoint, no active span");
+        }
+    }
+
+    /**
+     * Rollback will not guarantee in multi thread scenarios.
+     * As TracingContext expected, it should be in a single thread only.
+     * If you need synchronization or lock with span finish, you should do it by yourself.
+     *
+     * @param savepoint for rollback to.
+     */
+    @Override public void rollback(ISavepoint savepoint) {
+        if (savepoint instanceof TracingSavepoint) {
+            TracingSavepoint tracingSavepoint = (TracingSavepoint)savepoint;
+            if (tracingSavepoint.getOwner() == this) {
+                boolean validSavepoint = false;
+                for (AbstractSpan span : this.activeSpanStack) {
+                    if (span == tracingSavepoint.getStopPoint()) {
+                        validSavepoint = true;
+                        break;
+                    }
+                }
+
+                if (validSavepoint) {
+                    throw new IllegalArgumentException("Savepoint is expired.");
+                }
+
+                while (tracingSavepoint.getStopPoint() != peek()) {
+                    AbstractSpan activeSpan = peek();
+                    this.stopSpan(activeSpan);
+                }
+            } else {
+                throw new IllegalArgumentException("TracingContext supports to rollback to its own savepoint only.");
+            }
+        }
+        throw new IllegalArgumentException("TracingContext supports savepoint in TracingSavepoint type only");
+    }
+
     /**
      * Finish this context, and notify all {@link TracingContextListener}s, managed by {@link
      * TracingContext.ListenerManager}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/savepoint/ISavepoint.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/savepoint/ISavepoint.java
new file mode 100644
index 0000000..e6516bf
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/savepoint/ISavepoint.java
@@ -0,0 +1,28 @@
+/*
+ * 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.context.savepoint;
+
+/**
+ * Context savepoint usually includes a parent span in current active span stack,
+ * It could be used to rollback the context to the given point.
+ *
+ * @author wusheng
+ */
+public interface ISavepoint {
+}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/savepoint/TracingSavepoint.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/savepoint/TracingSavepoint.java
new file mode 100644
index 0000000..a4086bb
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/savepoint/TracingSavepoint.java
@@ -0,0 +1,43 @@
+/*
+ * 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.context.savepoint;
+
+import org.apache.skywalking.apm.agent.core.context.TracingContext;
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan;
+
+/**
+ * @author wusheng
+ */
+public class TracingSavepoint implements ISavepoint {
+    private TracingContext owner;
+    private AbstractSpan stopPoint;
+
+    public TracingSavepoint(TracingContext owner, AbstractSpan stopPoint) {
+        this.owner = owner;
+        this.stopPoint = stopPoint;
+    }
+
+    public TracingContext getOwner() {
+        return owner;
+    }
+
+    public AbstractSpan getStopPoint() {
+        return stopPoint;
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
wusheng@apache.org.