chyezh 3563136c2a
enhance: timetick interceptor implementation (#34238)
issue: #33285

- optimize the message package
- add interceptor package to achieve append operation intercepting.
- add timetick interceptor to attach timetick properties for message.
- add timetick background task to send timetick message.

Signed-off-by: chyezh <chyezh@outlook.com>
2024-07-02 14:42:08 +08:00

89 lines
2.5 KiB
Go

// Licensed to the LF AI & Data foundation 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 timestamp
import (
"context"
"sync"
"github.com/milvus-io/milvus/internal/types"
)
// batchAllocateSize is the size of batch allocate from remote allocator.
const batchAllocateSize = 1000
var _ Allocator = (*allocatorImpl)(nil)
// NewAllocator creates a new allocator.
func NewAllocator(rc types.RootCoordClient) Allocator {
return &allocatorImpl{
mu: sync.Mutex{},
remoteAllocator: newRemoteAllocator(rc),
localAllocator: newLocalAllocator(),
}
}
type Allocator interface {
// Allocate allocates a timestamp.
Allocate(ctx context.Context) (uint64, error)
// Sync expire the local allocator messages,
// syncs the local allocator and remote allocator.
Sync()
}
type allocatorImpl struct {
mu sync.Mutex
remoteAllocator *remoteAllocator
localAllocator *localAllocator
}
// AllocateOne allocates a timestamp.
func (ta *allocatorImpl) Allocate(ctx context.Context) (uint64, error) {
ta.mu.Lock()
defer ta.mu.Unlock()
// allocate one from local allocator first.
if id, err := ta.localAllocator.allocateOne(); err == nil {
return id, nil
}
// allocate from remote.
return ta.allocateRemote(ctx)
}
// Sync expire the local allocator messages,
// syncs the local allocator and remote allocator.
func (ta *allocatorImpl) Sync() {
ta.mu.Lock()
defer ta.mu.Unlock()
ta.localAllocator.exhausted()
}
// allocateRemote allocates timestamp from remote root coordinator.
func (ta *allocatorImpl) allocateRemote(ctx context.Context) (uint64, error) {
// Update local allocator from remote.
start, count, err := ta.remoteAllocator.allocate(ctx, batchAllocateSize)
if err != nil {
return 0, err
}
ta.localAllocator.update(start, count)
// Get from local again.
return ta.localAllocator.allocateOne()
}