morrySnow commented on code in PR #13659: URL: https://github.com/apache/doris/pull/13659#discussion_r1021497867
########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventConsumer.java: ########## @@ -0,0 +1,39 @@ +// 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; + +/** + * consumer + */ +public class EventConsumer { Review Comment: abstact ########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventConsumer.java: ########## @@ -0,0 +1,39 @@ +// 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; + +/** + * consumer + */ +public class EventConsumer { + private final Class<? extends Event> targetClass; + + protected EventConsumer(Class<? extends Event> targetClass) { + this.targetClass = targetClass; + } + + public void consume(Event event) { + } Review Comment: add comment to interface ########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventConsumer.java: ########## @@ -0,0 +1,39 @@ +// 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; + +/** + * consumer + */ +public class EventConsumer { + private final Class<? extends Event> targetClass; + + protected EventConsumer(Class<? extends Event> targetClass) { + this.targetClass = targetClass; + } + + public void consume(Event event) { + } + + public Class<? extends Event> getTargetClass() { + return targetClass; + } + + public void close() { Review Comment: ditto ########## fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/Job.java: ########## @@ -89,47 +91,33 @@ public List<Rule> getValidRules(GroupExpression groupExpression, public abstract void execute() throws AnalysisException; - protected Optional<CopyInResult> invokeRewriteRuleWithTrace(Rule rule, Plan before, Group targetGroup) { + protected Optional<CopyInResult> invokeRewriteRuleWithTrace(Rule rule, Plan before, Group targetGroup, + EventProducer transformTracer) { context.onInvokeRule(rule.getRuleType()); - - String traceBefore = enableTrace ? getPlanTraceLog() : null; + COUNTER_TRACER.log(new CounterEvent(Memo.getStateId(), + CounterType.EXPRESSION_TRANSFORM, targetGroup, targetGroup.getLogicalExpression(), before)); List<Plan> afters = rule.transform(before, context.getCascadesContext()); Preconditions.checkArgument(afters.size() == 1); Plan after = afters.get(0); - - if (after != before) { - CopyInResult result = context.getCascadesContext() - .getMemo() - .copyIn(after, targetGroup, true); - - if (result.generateNewExpression && enableTrace) { - String traceAfter = getPlanTraceLog(); - printTraceLog(rule, traceBefore, traceAfter); - } - - return Optional.of(result); + if (after == before) { + return Optional.empty(); Review Comment: why add this if? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventFilter.java: ########## @@ -0,0 +1,37 @@ +// 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 filter + */ +public class EventFilter { Review Comment: abstract ########## fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java: ########## @@ -588,6 +592,28 @@ public class SessionVariable implements Serializable, Writable { @VariableMgr.VarAttr(name = INTERNAL_SESSION) public boolean internalSession = false; + @VariableMgr.VarAttr(name = ENABLE_NEREIDS_EVENT) + public boolean enableNereidsEvent = false; Review Comment: cannot to see where to use it ########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventEnhancer.java: ########## @@ -0,0 +1,36 @@ +// 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 enhancer + */ +public class EventEnhancer { Review Comment: abstract ########## fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java: ########## @@ -222,6 +222,10 @@ public class SessionVariable implements Serializable, Writable { public static final String ENABLE_NEREIDS_STATS_DERIVE_V2 = "enable_nereids_stats_derive_v2"; + public static final String ENABLE_NEREIDS_EVENT = "enable_nereids_event"; Review Comment: use already exists session variables "enable_nereids_trace" ########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventChannel.java: ########## @@ -0,0 +1,124 @@ +// 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; + +/** + * 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 boolean isStop = false; Review Comment: need atomic? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/metrics/EventChannel.java: ########## @@ -0,0 +1,124 @@ +// 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; + +/** + * 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 boolean isStop = false; + private Thread thread; + + public void add(Event e) { + queue.add(e); + } + + public EventChannel addConsumers(List<EventConsumer> consumers) { + consumers.forEach(consumer -> this.consumers + .computeIfAbsent(consumer.getTargetClass(), k -> Lists.newArrayList()).add(consumer)); + return this; + } + + public EventChannel addEnhancers(List<EventEnhancer> enhancers) { + enhancers.forEach(enhancer -> this.enhancers.putIfAbsent(enhancer.getTargetClass(), enhancer)); + return this; + } + + public EventChannel setConnectContext(ConnectContext context) { + Preconditions.checkArgument(Objects.nonNull(context)); + eventSwitch = new EventSwitchParser().parse(context.getSessionVariable().nereidsEventMode); + return this; + } + + public Set<Class<? extends Event>> getEventSwitch() { + return eventSwitch; + } + + public static EventChannel getDefaultChannel() { + return DEFAULT_CHANNEL; + } + + private class Worker implements Runnable { + @Override + public void run() { + while (!isStop) { + try { + Event e = queue.poll(); + if (e == null) { + continue; + } + for (EventConsumer consumer : consumers.get(e.getClass())) { + if (enhancers.containsKey(e.getClass())) { + enhancers.get(e.getClass()).enhance(e); + } + consumer.consume(e.clone()); + } + } catch (Exception exception) { + LOG.warn("encounter exception when push event: ", exception); + } + } + for (List<EventConsumer> consumerList : consumers.values()) { + for (EventConsumer consumer : consumerList) { + consumer.close(); + } + } Review Comment: should we stop after consume all event? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
