You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2021/02/05 02:15:24 UTC

[GitHub] [servicecomb-service-center] robotLJW commented on a change in pull request #844: [SCB-2094] Add cache heartbeat mode in mongo

robotLJW commented on a change in pull request #844:
URL: https://github.com/apache/servicecomb-service-center/pull/844#discussion_r570674592



##########
File path: datasource/mongo/heartbeat/heartbeatcache/heartbeat.go
##########
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) 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 request 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 request 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 heartbeatcache
+
+import (
+	"context"
+	"fmt"
+	"runtime"
+	"time"
+
+	"github.com/patrickmn/go-cache"
+	"go.mongodb.org/mongo-driver/bson"
+
+	"github.com/apache/servicecomb-service-center/datasource/mongo"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
+	"github.com/apache/servicecomb-service-center/pkg/gopool"
+	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/server/config"
+)
+
+const (
+	defaultTTL              = 30
+	instanceCheckerInternal = 1 * time.Second
+	ctxTimeout              = 5 * time.Second
+)
+
+// Store cache structure
+type instanceHeartbeatInfo struct {
+	serviceID   string
+	instanceID  string
+	ttl         int32
+	lastRefresh time.Time
+}
+
+var (
+	cacheChan              chan *instanceHeartbeatInfo
+	instanceHeartbeatStore = cache.New(0, instanceCheckerInternal)
+	workerNum              = runtime.NumCPU()
+)
+
+func init() {
+	capacity := config.GetInt("heartbeat.cache.capacity", 10000, config.WithStandby("heartbeat_cache_capacity"))
+	cacheChan = make(chan *instanceHeartbeatInfo, capacity)
+	num := config.GetInt("heartbeat.worker.num", 10, config.WithStandby("heartbeat_worker_num"))
+	if num != 0 {
+		workerNum = num
+	}
+	instanceHeartbeatStore.OnEvicted(func(k string, v interface{}) {
+		instanceInfo, ok := v.(*instanceHeartbeatInfo)
+		if ok && instanceInfo != nil {
+			ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
+			defer cancel()
+			err := cleanInstance(ctx, instanceInfo.serviceID, instanceInfo.instanceID)
+			if err != nil {
+				log.Error("failed to cleanInstance in mongodb.", err)
+			}
+		}
+	})
+
+	for i := 1; i <= workerNum; i++ {
+		gopool.Go(func(ctx context.Context) {
+			for {
+				select {
+				case <-ctx.Done():
+					return
+				case heartbeatInfo, ok := <-cacheChan:
+					if ok {
+						instanceHeartbeatStore.Set(heartbeatInfo.instanceID, heartbeatInfo, time.Duration(heartbeatInfo.ttl)*time.Second)

Review comment:
       这个不是变量是从结构体里面获取的由于格式要求,需要转换下格式




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org