|
| 1 | +package adaptor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "go.uber.org/zap" |
| 7 | + |
| 8 | + "github.com/milvus-io/milvus/internal/streamingnode/server/wal" |
| 9 | + "github.com/milvus-io/milvus/internal/streamingnode/server/wal/metricsutil" |
| 10 | + "github.com/milvus-io/milvus/internal/util/streamingutil/status" |
| 11 | + "github.com/milvus-io/milvus/pkg/v2/log" |
| 12 | + "github.com/milvus-io/milvus/pkg/v2/streaming/util/message" |
| 13 | + "github.com/milvus-io/milvus/pkg/v2/streaming/util/types" |
| 14 | + "github.com/milvus-io/milvus/pkg/v2/streaming/walimpls" |
| 15 | + "github.com/milvus-io/milvus/pkg/v2/util/typeutil" |
| 16 | +) |
| 17 | + |
| 18 | +var _ wal.WAL = (*roWALAdaptorImpl)(nil) |
| 19 | + |
| 20 | +type roWALAdaptorImpl struct { |
| 21 | + log.Binder |
| 22 | + lifetime *typeutil.Lifetime |
| 23 | + available chan struct{} |
| 24 | + idAllocator *typeutil.IDAllocator |
| 25 | + roWALImpls walimpls.ROWALImpls |
| 26 | + scannerRegistry scannerRegistry |
| 27 | + scanners *typeutil.ConcurrentMap[int64, wal.Scanner] |
| 28 | + cleanup func() |
| 29 | + scanMetrics *metricsutil.ScanMetrics |
| 30 | +} |
| 31 | + |
| 32 | +func (w *roWALAdaptorImpl) WALName() string { |
| 33 | + return w.roWALImpls.WALName() |
| 34 | +} |
| 35 | + |
| 36 | +// Channel returns the channel info of wal. |
| 37 | +func (w *roWALAdaptorImpl) Channel() types.PChannelInfo { |
| 38 | + return w.roWALImpls.Channel() |
| 39 | +} |
| 40 | + |
| 41 | +func (w *roWALAdaptorImpl) GetLatestMVCCTimestamp(ctx context.Context, vchannel string) (uint64, error) { |
| 42 | + panic("we cannot acquire lastest mvcc timestamp from a read only wal") |
| 43 | +} |
| 44 | + |
| 45 | +// Append writes a record to the log. |
| 46 | +func (w *roWALAdaptorImpl) Append(ctx context.Context, msg message.MutableMessage) (*wal.AppendResult, error) { |
| 47 | + panic("we cannot append message into a read only wal") |
| 48 | +} |
| 49 | + |
| 50 | +// Append a record to the log asynchronously. |
| 51 | +func (w *roWALAdaptorImpl) AppendAsync(ctx context.Context, msg message.MutableMessage, cb func(*wal.AppendResult, error)) { |
| 52 | + panic("we cannot append message into a read only wal") |
| 53 | +} |
| 54 | + |
| 55 | +// Read returns a scanner for reading records from the wal. |
| 56 | +func (w *roWALAdaptorImpl) Read(ctx context.Context, opts wal.ReadOption) (wal.Scanner, error) { |
| 57 | + if !w.lifetime.Add(typeutil.LifetimeStateWorking) { |
| 58 | + return nil, status.NewOnShutdownError("wal is on shutdown") |
| 59 | + } |
| 60 | + defer w.lifetime.Done() |
| 61 | + |
| 62 | + name, err := w.scannerRegistry.AllocateScannerName() |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + // wrap the scanner with cleanup function. |
| 67 | + id := w.idAllocator.Allocate() |
| 68 | + s := newScannerAdaptor( |
| 69 | + name, |
| 70 | + w.roWALImpls, |
| 71 | + opts, |
| 72 | + w.scanMetrics.NewScannerMetrics(), |
| 73 | + func() { w.scanners.Remove(id) }) |
| 74 | + w.scanners.Insert(id, s) |
| 75 | + return s, nil |
| 76 | +} |
| 77 | + |
| 78 | +// IsAvailable returns whether the wal is available. |
| 79 | +func (w *roWALAdaptorImpl) IsAvailable() bool { |
| 80 | + select { |
| 81 | + case <-w.available: |
| 82 | + return false |
| 83 | + default: |
| 84 | + return true |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Available returns a channel that will be closed when the wal is shut down. |
| 89 | +func (w *roWALAdaptorImpl) Available() <-chan struct{} { |
| 90 | + return w.available |
| 91 | +} |
| 92 | + |
| 93 | +// Close overrides Scanner Close function. |
| 94 | +func (w *roWALAdaptorImpl) Close() { |
| 95 | + // begin to close the wal. |
| 96 | + w.Logger().Info("wal begin to close...") |
| 97 | + w.lifetime.SetState(typeutil.LifetimeStateStopped) |
| 98 | + w.lifetime.Wait() |
| 99 | + close(w.available) |
| 100 | + |
| 101 | + w.Logger().Info("wal begin to close scanners...") |
| 102 | + |
| 103 | + // close all wal instances. |
| 104 | + w.scanners.Range(func(id int64, s wal.Scanner) bool { |
| 105 | + s.Close() |
| 106 | + log.Info("close scanner by wal adaptor", zap.Int64("id", id), zap.Any("channel", w.Channel())) |
| 107 | + return true |
| 108 | + }) |
| 109 | + |
| 110 | + w.Logger().Info("scanner close done, close inner wal...") |
| 111 | + w.roWALImpls.Close() |
| 112 | + |
| 113 | + w.Logger().Info("call wal cleanup function...") |
| 114 | + w.cleanup() |
| 115 | + w.Logger().Info("wal closed") |
| 116 | + |
| 117 | + // close all metrics. |
| 118 | + w.scanMetrics.Close() |
| 119 | +} |
0 commit comments