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



##########
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
+               } else {
+                       s.millisSinceLastTransition += t
+               }
+
+               if s.millisSinceLastTransition > s.nextLogTime {
+                       log.Info(ctx, "...standard long running operation log 
...", ps.pid, ps.state, s.millisSinceLastTransition)
+                       s.nextLogTime += s.logInterval
+               }
+       }
+}
+
+func loadCurrentState(s *StateSampler) currentStateVal {
+       if ts := 
(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&s.store.bundleState)))); 
ts == nil {
+               return currentStateVal{}
+       } else {
+               bs := *(*BundleState)(ts)
+               return currentStateVal{pid: bs.pid, state: bs.currentState, 
transitions: atomic.LoadInt64(s.store.transitions)}
+       }

Review comment:
       Minor style nit: If the `if` case returns, don't have an `else`, just 
have the contents of the else, un-indented. This improves readability.

##########
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
+               } else {
+                       s.millisSinceLastTransition += t
+               }
+
+               if s.millisSinceLastTransition > s.nextLogTime {
+                       log.Info(ctx, "...standard long running operation log 
...", ps.pid, ps.state, s.millisSinceLastTransition)

Review comment:
       The point of me commenting "...standard long running operation log..." 
meant that you should have looked up the log message that the  Python and Java 
SDKs use and add it here. I can't do all the research and reading for you. If 
you need help, I'll provide it, but please ask for it.
   Part of the job is to do your own thinking about the code you're writing, 
not simply responding to reviewers without thinking.

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

##########
File path: sdks/go/pkg/beam/core/metrics/store.go
##########
@@ -91,6 +91,11 @@ type Extractor struct {
        DistributionInt64 func(labels Labels, count, sum, min, max int64)
        // GaugeInt64 extracts data from Gauge Int64 counters.
        GaugeInt64 func(labels Labels, v int64, t time.Time)
+
+       // Extraction of Msec counters is experimental and subject to change.
+

Review comment:
       Remove the blank line. Blank lines disconnect comments and prevent them 
from being surfaced in the documentation. If you want to have a documented 
paragraph break, the "blank" line still needs a `//` at it's start.
   
   At this point the comment should be
   ```
   // MsecsInt64 extracts data from StateRegistry of ExecutionState.
   //
   // Extraction of Msec counters is experimental and subject to change.
   ```




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