2021-12-29 09:48:04 +00: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 05:42:47 +00:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-29 09:48:04 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 05:42:47 +00:00
|
|
|
//
|
2021-12-29 09:48:04 +00: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 05:42:47 +00:00
|
|
|
|
2022-03-03 13:57:56 +00:00
|
|
|
package server
|
2021-01-26 01:38:40 +00:00
|
|
|
|
2021-02-09 07:57:10 +00:00
|
|
|
import (
|
2021-12-02 02:27:32 +00:00
|
|
|
"errors"
|
2021-06-26 04:28:07 +00:00
|
|
|
"os"
|
2021-11-08 02:17:18 +00:00
|
|
|
"strconv"
|
2021-02-09 07:57:10 +00:00
|
|
|
"sync"
|
2021-09-06 11:36:42 +00:00
|
|
|
"sync/atomic"
|
2021-02-09 07:57:10 +00:00
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
2021-08-16 10:46:10 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2021-02-24 09:12:06 +00:00
|
|
|
|
2021-11-15 11:24:52 +00:00
|
|
|
"go.uber.org/zap"
|
2021-02-09 07:57:10 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 10:29:56 +00:00
|
|
|
// Rmq is global rocksmq instance that will be initialized only once
|
2021-03-19 11:33:21 +00:00
|
|
|
var Rmq *rocksmq
|
2021-09-24 13:45:56 +00:00
|
|
|
|
|
|
|
// once is used to init global rocksmq
|
2021-02-09 07:57:10 +00:00
|
|
|
var once sync.Once
|
2021-09-24 13:45:56 +00:00
|
|
|
|
|
|
|
// Params provide params that rocksmq needs
|
2021-08-16 10:46:10 +00:00
|
|
|
var params paramtable.BaseTable
|
2021-02-03 09:30:10 +00:00
|
|
|
|
2021-09-24 13:45:56 +00:00
|
|
|
// InitRmq is deprecate implementation of global rocksmq. will be removed later
|
2021-02-24 09:12:06 +00:00
|
|
|
func InitRmq(rocksdbName string, idAllocator allocator.GIDAllocator) error {
|
2021-01-26 01:38:40 +00:00
|
|
|
var err error
|
2022-06-20 02:56:12 +00:00
|
|
|
params.Init()
|
|
|
|
Rmq, err = NewRocksMQ(params, rocksdbName, idAllocator)
|
2021-01-26 01:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-24 13:45:56 +00:00
|
|
|
// InitRocksMQ init global rocksmq single instance
|
2022-04-07 14:05:32 +00:00
|
|
|
func InitRocksMQ(path string) error {
|
2021-12-02 02:27:32 +00:00
|
|
|
var finalErr error
|
2021-02-09 07:57:10 +00:00
|
|
|
once.Do(func() {
|
2021-08-16 10:46:10 +00:00
|
|
|
params.Init()
|
2022-04-07 14:05:32 +00:00
|
|
|
log.Debug("initializing global rmq", zap.String("path", path))
|
2021-12-02 02:27:32 +00:00
|
|
|
var fi os.FileInfo
|
2022-04-07 14:05:32 +00:00
|
|
|
fi, finalErr = os.Stat(path)
|
2021-12-02 02:27:32 +00:00
|
|
|
if os.IsNotExist(finalErr) {
|
2022-04-07 14:05:32 +00:00
|
|
|
finalErr = os.MkdirAll(path, os.ModePerm)
|
2021-12-02 02:27:32 +00:00
|
|
|
if finalErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
errMsg := "can't create a directory because there exists a file with the same name"
|
|
|
|
finalErr = errors.New(errMsg)
|
|
|
|
return
|
2021-06-26 04:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:17:18 +00:00
|
|
|
rawRmqPageSize, err := params.Load("rocksmq.rocksmqPageSize")
|
|
|
|
if err == nil && rawRmqPageSize != "" {
|
|
|
|
rmqPageSize, err := strconv.ParseInt(rawRmqPageSize, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
atomic.StoreInt64(&RocksmqPageSize, rmqPageSize)
|
|
|
|
} else {
|
|
|
|
log.Warn("rocksmq.rocksmqPageSize is invalid, using default value 2G")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rawRmqRetentionTimeInMinutes, err := params.Load("rocksmq.retentionTimeInMinutes")
|
|
|
|
if err == nil && rawRmqRetentionTimeInMinutes != "" {
|
|
|
|
rawRmqRetentionTimeInMinutes, err := strconv.ParseInt(rawRmqRetentionTimeInMinutes, 10, 64)
|
|
|
|
if err == nil {
|
2021-12-17 15:44:42 +00:00
|
|
|
atomic.StoreInt64(&RocksmqRetentionTimeInSecs, rawRmqRetentionTimeInMinutes*60)
|
2021-11-08 02:17:18 +00:00
|
|
|
} else {
|
2021-12-30 12:08:15 +00:00
|
|
|
log.Warn("rocksmq.retentionTimeInMinutes is invalid, using default value")
|
2021-11-08 02:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rawRmqRetentionSizeInMB, err := params.Load("rocksmq.retentionSizeInMB")
|
|
|
|
if err == nil && rawRmqRetentionSizeInMB != "" {
|
|
|
|
rawRmqRetentionSizeInMB, err := strconv.ParseInt(rawRmqRetentionSizeInMB, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
atomic.StoreInt64(&RocksmqRetentionSizeInMB, rawRmqRetentionSizeInMB)
|
|
|
|
} else {
|
|
|
|
log.Warn("rocksmq.retentionSizeInMB is invalid, using default value 0")
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 15:44:42 +00:00
|
|
|
log.Debug("", zap.Any("RocksmqRetentionTimeInMinutes", rawRmqRetentionTimeInMinutes),
|
2021-11-08 02:17:18 +00:00
|
|
|
zap.Any("RocksmqRetentionSizeInMB", RocksmqRetentionSizeInMB), zap.Any("RocksmqPageSize", RocksmqPageSize))
|
2022-06-20 02:56:12 +00:00
|
|
|
Rmq, finalErr = NewRocksMQ(params, path, nil)
|
2021-02-09 07:57:10 +00:00
|
|
|
})
|
2021-12-02 02:27:32 +00:00
|
|
|
return finalErr
|
2021-02-09 07:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 13:45:56 +00:00
|
|
|
// CloseRocksMQ is used to close global rocksmq
|
2021-02-09 07:57:10 +00:00
|
|
|
func CloseRocksMQ() {
|
2021-08-16 10:46:10 +00:00
|
|
|
log.Debug("Close Rocksmq!")
|
2021-09-22 09:21:00 +00:00
|
|
|
if Rmq != nil && Rmq.store != nil {
|
|
|
|
Rmq.Close()
|
2021-02-09 07:57:10 +00:00
|
|
|
}
|
2021-01-26 01:38:40 +00:00
|
|
|
}
|