2021-04-19 05:42:47 +00:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2020-10-29 01:31:08 +00:00
|
|
|
package tsoutil
|
|
|
|
|
|
|
|
import (
|
2020-11-12 03:18:23 +00:00
|
|
|
"path"
|
2020-10-29 01:31:08 +00:00
|
|
|
"time"
|
2020-11-12 03:18:23 +00:00
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
etcdkv "github.com/milvus-io/milvus/internal/kv/etcd"
|
2020-11-12 03:18:23 +00:00
|
|
|
"go.etcd.io/etcd/clientv3"
|
2020-10-29 01:31:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-04-12 06:11:06 +00:00
|
|
|
logicalBits = 18
|
|
|
|
logicalBitsMask = (1 << logicalBits) - 1
|
2020-10-29 01:31:08 +00:00
|
|
|
)
|
|
|
|
|
2020-11-03 06:53:36 +00:00
|
|
|
func ComposeTS(physical, logical int64) uint64 {
|
2021-04-12 06:11:06 +00:00
|
|
|
return uint64((physical << logicalBits) + logical)
|
2020-10-30 08:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 01:31:08 +00:00
|
|
|
// ParseTS parses the ts to (physical,logical).
|
|
|
|
func ParseTS(ts uint64) (time.Time, uint64) {
|
2021-04-12 06:11:06 +00:00
|
|
|
logical := ts & logicalBitsMask
|
|
|
|
physical := ts >> logicalBits
|
2020-10-29 01:31:08 +00:00
|
|
|
physicalTime := time.Unix(int64(physical/1000), int64(physical)%1000*time.Millisecond.Nanoseconds())
|
|
|
|
return physicalTime, logical
|
|
|
|
}
|
2020-11-12 03:18:23 +00:00
|
|
|
|
2020-12-07 07:22:20 +00:00
|
|
|
func NewTSOKVBase(etcdAddr []string, tsoRoot, subPath string) *etcdkv.EtcdKV {
|
2020-11-12 03:18:23 +00:00
|
|
|
client, _ := clientv3.New(clientv3.Config{
|
2020-11-16 09:01:10 +00:00
|
|
|
Endpoints: etcdAddr,
|
2020-11-12 03:18:23 +00:00
|
|
|
DialTimeout: 5 * time.Second,
|
|
|
|
})
|
2020-12-07 07:22:20 +00:00
|
|
|
return etcdkv.NewEtcdKV(client, path.Join(tsoRoot, subPath))
|
2020-11-12 03:18:23 +00:00
|
|
|
}
|