2021-10-14 19:27:11 +08:00
|
|
|
// 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
|
2021-04-19 15:15:33 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-14 19:27:11 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 15:15:33 +08:00
|
|
|
//
|
2021-10-14 19:27:11 +08:00
|
|
|
// 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.
|
2021-04-19 15:15:33 +08:00
|
|
|
|
2021-02-24 17:12:06 +08:00
|
|
|
package allocator
|
2021-01-19 14:44:03 +08:00
|
|
|
|
|
|
|
import (
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/tso"
|
2024-06-25 21:18:15 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/kv"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2021-01-19 14:44:03 +08:00
|
|
|
)
|
|
|
|
|
2024-10-17 12:15:25 +08:00
|
|
|
type GlobalIDAllocatorInterface interface {
|
|
|
|
// Initialize will initialize the created global TSO allocator.
|
|
|
|
Initialize() error
|
|
|
|
// Alloc allocates the id of the count number.
|
|
|
|
Alloc(count uint32) (typeutil.UniqueID, typeutil.UniqueID, error)
|
|
|
|
// AllocOne allocates one id.
|
|
|
|
AllocOne() (typeutil.UniqueID, error)
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:20:18 +08:00
|
|
|
// GlobalIDAllocator is the global single point TSO allocator.
|
2021-01-19 14:44:03 +08:00
|
|
|
type GlobalIDAllocator struct {
|
2021-02-24 17:12:06 +08:00
|
|
|
allocator tso.Allocator
|
2021-01-19 14:44:03 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 10:24:31 +08:00
|
|
|
// NewGlobalIDAllocator creates GlobalIDAllocator for allocates ID.
|
2021-04-12 18:09:28 +08:00
|
|
|
func NewGlobalIDAllocator(key string, base kv.TxnKV) *GlobalIDAllocator {
|
2021-02-26 13:05:52 +08:00
|
|
|
allocator := tso.NewGlobalTSOAllocator(key, base)
|
2021-04-12 14:11:06 +08:00
|
|
|
allocator.SetLimitMaxLogic(false)
|
2021-01-19 14:44:03 +08:00
|
|
|
return &GlobalIDAllocator{
|
2021-02-26 13:05:52 +08:00
|
|
|
allocator: allocator,
|
2021-01-19 14:44:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize will initialize the created global TSO allocator.
|
|
|
|
func (gia *GlobalIDAllocator) Initialize() error {
|
|
|
|
return gia.allocator.Initialize()
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:20:18 +08:00
|
|
|
// Alloc allocates the id of the count number.
|
2021-01-19 14:44:03 +08:00
|
|
|
// GenerateTSO is used to generate a given number of TSOs.
|
|
|
|
// Make sure you have initialized the TSO allocator before calling.
|
|
|
|
func (gia *GlobalIDAllocator) Alloc(count uint32) (typeutil.UniqueID, typeutil.UniqueID, error) {
|
|
|
|
timestamp, err := gia.allocator.GenerateTSO(count)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2021-07-14 17:11:54 +08:00
|
|
|
idEnd := typeutil.UniqueID(timestamp) + 1
|
|
|
|
idStart := idEnd - int64(count)
|
2021-01-19 14:44:03 +08:00
|
|
|
return idStart, idEnd, nil
|
|
|
|
}
|
|
|
|
|
2021-10-01 10:24:31 +08:00
|
|
|
// AllocOne allocates one id.
|
2021-01-19 14:44:03 +08:00
|
|
|
func (gia *GlobalIDAllocator) AllocOne() (typeutil.UniqueID, error) {
|
|
|
|
timestamp, err := gia.allocator.GenerateTSO(1)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
idStart := typeutil.UniqueID(timestamp)
|
|
|
|
return idStart, nil
|
|
|
|
}
|