morrySnow commented on code in PR #13659:
URL: https://github.com/apache/doris/pull/13659#discussion_r1028304577


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventSwitchParser.java:
##########
@@ -0,0 +1,78 @@
+// 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.doris.nereids.metrics;
+
+import org.apache.doris.nereids.metrics.event.CostStateUpdateEvent;
+import org.apache.doris.nereids.metrics.event.CounterEvent;
+import org.apache.doris.nereids.metrics.event.EnforcerEvent;
+import org.apache.doris.nereids.metrics.event.FunctionCallEvent;
+import org.apache.doris.nereids.metrics.event.GroupMergeEvent;
+import org.apache.doris.nereids.metrics.event.StatsStateEvent;
+import org.apache.doris.nereids.metrics.event.TransformEvent;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * parser
+ */
+public class EventSwitchParser {
+    private static final Map<String, Class<? extends Event>> EVENT_TYPE_SET =
+            new Builder<String, Class<? extends Event>>()
+            .put("costState", CostStateUpdateEvent.class)
+            .put("counter", CounterEvent.class)
+            .put("enforcer", EnforcerEvent.class)
+            .put("functionCall", FunctionCallEvent.class)
+            .put("groupMerge", GroupMergeEvent.class)
+            .put("statsState", StatsStateEvent.class)
+            .put("transform", TransformEvent.class)
+            .build();
+
+    /**
+     * parse
+     */
+    public Set<Class<? extends Event>> parse(String eventTypeMode) {
+        List<String> strings = Arrays.stream(eventTypeMode.split("[ ,]"))

Review Comment:
   nit: use \s to replace white blank



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/PrintConsumer.java:
##########
@@ -0,0 +1,41 @@
+// 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.doris.nereids.metrics;
+
+import java.io.PrintStream;
+
+/**
+ * print consumer
+ */
+public class PrintConsumer extends EventConsumer {

Review Comment:
   put them into a sub package `consumer`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventSwitchParser.java:
##########
@@ -0,0 +1,78 @@
+// 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.doris.nereids.metrics;
+
+import org.apache.doris.nereids.metrics.event.CostStateUpdateEvent;
+import org.apache.doris.nereids.metrics.event.CounterEvent;
+import org.apache.doris.nereids.metrics.event.EnforcerEvent;
+import org.apache.doris.nereids.metrics.event.FunctionCallEvent;
+import org.apache.doris.nereids.metrics.event.GroupMergeEvent;
+import org.apache.doris.nereids.metrics.event.StatsStateEvent;
+import org.apache.doris.nereids.metrics.event.TransformEvent;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * parser
+ */
+public class EventSwitchParser {
+    private static final Map<String, Class<? extends Event>> EVENT_TYPE_SET =
+            new Builder<String, Class<? extends Event>>()
+            .put("costState", CostStateUpdateEvent.class)
+            .put("counter", CounterEvent.class)
+            .put("enforcer", EnforcerEvent.class)
+            .put("functionCall", FunctionCallEvent.class)
+            .put("groupMerge", GroupMergeEvent.class)
+            .put("statsState", StatsStateEvent.class)
+            .put("transform", TransformEvent.class)
+            .build();
+
+    /**
+     * parse
+     */
+    public Set<Class<? extends Event>> parse(String eventTypeMode) {
+        List<String> strings = Arrays.stream(eventTypeMode.split("[ ,]"))
+                .map(String::trim)
+                .collect(Collectors.toList());
+        Preconditions.checkArgument(strings.size() > 0);
+        if ("all".equals(strings.get(0))) {

Review Comment:
   use equalsIgnoreCase or use eventTypeMode.toLowerCase at beginning



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventSwitchParser.java:
##########
@@ -0,0 +1,78 @@
+// 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.doris.nereids.metrics;
+
+import org.apache.doris.nereids.metrics.event.CostStateUpdateEvent;
+import org.apache.doris.nereids.metrics.event.CounterEvent;
+import org.apache.doris.nereids.metrics.event.EnforcerEvent;
+import org.apache.doris.nereids.metrics.event.FunctionCallEvent;
+import org.apache.doris.nereids.metrics.event.GroupMergeEvent;
+import org.apache.doris.nereids.metrics.event.StatsStateEvent;
+import org.apache.doris.nereids.metrics.event.TransformEvent;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * parser
+ */
+public class EventSwitchParser {
+    private static final Map<String, Class<? extends Event>> EVENT_TYPE_SET =
+            new Builder<String, Class<? extends Event>>()
+            .put("costState", CostStateUpdateEvent.class)
+            .put("counter", CounterEvent.class)
+            .put("enforcer", EnforcerEvent.class)
+            .put("functionCall", FunctionCallEvent.class)
+            .put("groupMerge", GroupMergeEvent.class)
+            .put("statsState", StatsStateEvent.class)
+            .put("transform", TransformEvent.class)
+            .build();
+
+    /**
+     * parse
+     */
+    public Set<Class<? extends Event>> parse(String eventTypeMode) {
+        List<String> strings = Arrays.stream(eventTypeMode.split("[ ,]"))
+                .map(String::trim)
+                .collect(Collectors.toList());
+        Preconditions.checkArgument(strings.size() > 0);
+        if ("all".equals(strings.get(0))) {
+            if (strings.size() == 1) {
+                return ImmutableSet.copyOf(EVENT_TYPE_SET.values());
+            }
+            Preconditions.checkArgument(strings.size() > 2 && 
"except".equals(strings.get(1)));

Review Comment:
   u should reject invalid event switch when user set variables, should not 
check in here



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/event/CostStateUpdateEvent.java:
##########
@@ -0,0 +1,49 @@
+// 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.doris.nereids.metrics.event;
+
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+
+/**
+ * cost state event
+ */
+public class CostStateUpdateEvent extends StateEvent {
+    private final double cost;
+    private final PhysicalProperties physicalProperties;
+
+    private CostStateUpdateEvent(GroupExpression groupExpression, double cost, 
PhysicalProperties physicalProperties) {
+        super(groupExpression);
+        this.cost = cost;
+        this.physicalProperties = physicalProperties;
+    }
+
+    public static CostStateUpdateEvent of(GroupExpression groupExpression, 
double cost,
+            PhysicalProperties physicalProperties) {
+        return checkConnectContext() ? new 
CostStateUpdateEvent(groupExpression, cost, physicalProperties) : null;
+    }
+
+    @Override
+    public String toString() {
+        return "CostStateEvent{"
+                + "groupExpression=" + getGroupExpression()
+                + ", cost=" + cost
+                + ", physicalProperties=" + physicalProperties
+                + '}';

Review Comment:
   maybe u could use `Utils.toSqlString`, just rename this function to a proper 
name



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/Memo.java:
##########
@@ -81,11 +94,11 @@ public Map<GroupExpression, GroupExpression> 
getGroupExpressions() {
      *                       is the corresponding group expression of the plan
      */
     public CopyInResult copyIn(Plan plan, @Nullable Group target, boolean 
rewrite) {
-        if (rewrite) {
-            return doRewrite(plan, target);
-        } else {
-            return doCopyIn(plan, target);
+        CopyInResult result = rewrite ? doRewrite(plan, target) : 
doCopyIn(plan, target);
+        if (result.generateNewExpression) {
+            stateId++;
         }

Review Comment:
   should not update stateId, if we do not need trace



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/event/CostStateUpdateEvent.java:
##########
@@ -0,0 +1,49 @@
+// 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.doris.nereids.metrics.event;
+
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+
+/**
+ * cost state event
+ */
+public class CostStateUpdateEvent extends StateEvent {
+    private final double cost;
+    private final PhysicalProperties physicalProperties;
+
+    private CostStateUpdateEvent(GroupExpression groupExpression, double cost, 
PhysicalProperties physicalProperties) {
+        super(groupExpression);
+        this.cost = cost;
+        this.physicalProperties = physicalProperties;
+    }
+
+    public static CostStateUpdateEvent of(GroupExpression groupExpression, 
double cost,
+            PhysicalProperties physicalProperties) {
+        return checkConnectContext() ? new 
CostStateUpdateEvent(groupExpression, cost, physicalProperties) : null;

Review Comment:
   all event should check whether need to trace it. If not, should not generate 
new object.



##########
fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java:
##########
@@ -606,6 +608,21 @@ public void initFuzzyModeVariables() {
         this.disableStreamPreaggregations = random.nextBoolean();
     }
 
+    @VariableMgr.VarAttr(name = NEREIDS_EVENT_MODE)
+    public String nereidsEventMode = "all";

Review Comment:
   add comment to enum all valid mode, and how to use it



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventChannel.java:
##########
@@ -0,0 +1,127 @@
+// 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.doris.nereids.metrics;
+
+import org.apache.doris.qe.ConnectContext;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * event channel
+ */
+public class EventChannel {
+    private static final Logger LOG = LogManager.getLogger(EventChannel.class);
+    private static final EventChannel DEFAULT_CHANNEL = new EventChannel();
+    private Set<Class<? extends Event>> eventSwitch = new HashSet<>();
+    private final Map<Class<? extends Event>, List<EventConsumer>> consumers = 
Maps.newHashMap();
+    private final Map<Class<? extends Event>, EventEnhancer> enhancers = 
Maps.newHashMap();
+    private final BlockingQueue<Event> queue = new LinkedBlockingQueue<>(4096);
+    private final AtomicBoolean isStop = new AtomicBoolean(false);
+    private Thread thread;
+
+    public void add(Event e) {
+        queue.add(e);
+    }
+
+    public synchronized EventChannel addConsumers(List<EventConsumer> 
consumers) {
+        consumers.forEach(consumer -> this.consumers
+                .computeIfAbsent(consumer.getTargetClass(), k -> 
Lists.newArrayList()).add(consumer));
+        return this;
+    }
+
+    public synchronized EventChannel addEnhancers(List<EventEnhancer> 
enhancers) {
+        enhancers.forEach(enhancer -> 
this.enhancers.putIfAbsent(enhancer.getTargetClass(), enhancer));
+        return this;
+    }
+
+    public synchronized EventChannel setConnectContext(ConnectContext context) 
{
+        Preconditions.checkArgument(Objects.nonNull(context));
+        if (context.getSessionVariable().isEnableNereidsTrace()) {
+            eventSwitch = new 
EventSwitchParser().parse(context.getSessionVariable().getNereidsEventMode());

Review Comment:
   eventSwitch always empty in real world, right?



-- 
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: commits-unsubscr...@doris.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to