lostluck commented on a change in pull request #15657:
URL: https://github.com/apache/beam/pull/15657#discussion_r738778127



##########
File path: sdks/go/pkg/beam/core/metrics/sampler.go
##########
@@ -0,0 +1,98 @@
+// 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 metrics
+
+import (
+       "context"
+       "sync/atomic"
+       "time"
+       "unsafe"
+
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/log"
+)
+
+// StateSampler tracks the state of a bundle.
+type StateSampler struct {
+       store                     *Store // used to store states into state 
registry
+       millisSinceLastTransition time.Duration
+       transitionsAtLastSample   int64
+       nextLogTime               time.Duration
+       logInterval               time.Duration
+}
+
+// NewSampler creates a new state sampler.
+func NewSampler(store *Store) StateSampler {
+       return StateSampler{store: store, nextLogTime: 5 * time.Minute, 
logInterval: 5 * time.Minute}
+}
+
+// Sample checks for state transition in processing a DoFn
+func (s *StateSampler) Sample(ctx context.Context, t time.Duration) {
+       ps := loadCurrentState(s)
+       if ps.pid == "" {
+               return
+       }
+       s.store.mu.Lock()
+       defer s.store.mu.Unlock()
+
+       if v, ok := s.store.stateRegistry[ps.pid]; ok {
+               v[ps.state].TotalTime += t
+               v[TotalBundle].TotalTime += t
+
+               if s.transitionsAtLastSample != ps.transitions {
+                       // state change detected
+                       s.millisSinceLastTransition = 0
+                       s.transitionsAtLastSample = ps.transitions

Review comment:
       We need to reset next log time here: `s.nextLogTime = s.logInterval` 
since a transition has happened.
   
   Otherwise, we end up with ever increasing nextLogTimes 5 minutes, 10 
minutes, 15 minutes etc. even if the state moves, the next "long running state" 
will take longer to produce the log.




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


Reply via email to