|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2025 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package executor |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "sync" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/rs/zerolog" |
| 29 | + |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/logging" |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 32 | +) |
| 33 | + |
| 34 | +func Run(ctx context.Context, log logging.Logger, threads int, f RunFunc) error { |
| 35 | + h := &handler{ |
| 36 | + th: NewThreadManager(threads), |
| 37 | + completed: make(chan struct{}), |
| 38 | + log: log, |
| 39 | + } |
| 40 | + |
| 41 | + go h.run(ctx, f) |
| 42 | + |
| 43 | + return h.Wait() |
| 44 | +} |
| 45 | + |
| 46 | +type RunFunc func(ctx context.Context, log logging.Logger, t Thread, h Handler) error |
| 47 | + |
| 48 | +type Executor interface { |
| 49 | + Completed() bool |
| 50 | + |
| 51 | + Wait() error |
| 52 | +} |
| 53 | + |
| 54 | +type Handler interface { |
| 55 | + RunAsync(ctx context.Context, f RunFunc) Executor |
| 56 | + |
| 57 | + WaitForSubThreads(t Thread) |
| 58 | +} |
| 59 | + |
| 60 | +type handler struct { |
| 61 | + lock sync.Mutex |
| 62 | + |
| 63 | + th ThreadManager |
| 64 | + |
| 65 | + handlers []*handler |
| 66 | + |
| 67 | + completed chan struct{} |
| 68 | + |
| 69 | + log logging.Logger |
| 70 | + |
| 71 | + err error |
| 72 | +} |
| 73 | + |
| 74 | +func (h *handler) WaitForSubThreads(t Thread) { |
| 75 | + for { |
| 76 | + t.Release() |
| 77 | + |
| 78 | + if h.subThreadsCompleted() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + time.Sleep(10 * time.Millisecond) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (h *handler) subThreadsCompleted() bool { |
| 87 | + h.lock.Lock() |
| 88 | + defer h.lock.Unlock() |
| 89 | + |
| 90 | + for id := range h.handlers { |
| 91 | + if !h.handlers[id].Completed() { |
| 92 | + return false |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return true |
| 97 | +} |
| 98 | + |
| 99 | +func (h *handler) Wait() error { |
| 100 | + <-h.completed |
| 101 | + |
| 102 | + return h.err |
| 103 | +} |
| 104 | + |
| 105 | +func (h *handler) Completed() bool { |
| 106 | + select { |
| 107 | + case <-h.completed: |
| 108 | + return true |
| 109 | + default: |
| 110 | + return false |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (h *handler) RunAsync(ctx context.Context, f RunFunc) Executor { |
| 115 | + h.lock.Lock() |
| 116 | + defer h.lock.Unlock() |
| 117 | + |
| 118 | + n := &handler{ |
| 119 | + th: h.th, |
| 120 | + completed: make(chan struct{}), |
| 121 | + log: h.log, |
| 122 | + } |
| 123 | + |
| 124 | + h.handlers = append(h.handlers, n) |
| 125 | + |
| 126 | + go n.run(ctx, f) |
| 127 | + |
| 128 | + return n |
| 129 | +} |
| 130 | + |
| 131 | +func (h *handler) run(ctx context.Context, entry RunFunc) { |
| 132 | + defer close(h.completed) |
| 133 | + |
| 134 | + err := h.runE(ctx, entry) |
| 135 | + |
| 136 | + subErrors := make([]error, len(h.handlers)) |
| 137 | + |
| 138 | + for id := range subErrors { |
| 139 | + subErrors[id] = h.handlers[id].Wait() |
| 140 | + } |
| 141 | + |
| 142 | + subError := errors.Errors(subErrors...) |
| 143 | + |
| 144 | + h.err = errors.Errors(err, subError) |
| 145 | +} |
| 146 | + |
| 147 | +func (h *handler) runE(ctx context.Context, entry RunFunc) error { |
| 148 | + t := h.th.Acquire() |
| 149 | + defer t.Release() |
| 150 | + |
| 151 | + log := h.log.Wrap(func(in *zerolog.Event) *zerolog.Event { |
| 152 | + return in.Int("thread", int(t.ID())) |
| 153 | + }) |
| 154 | + |
| 155 | + return entry(ctx, log, t, h) |
| 156 | +} |
0 commit comments