github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/chain.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package eurekaserver
    19  
    20  import (
    21  	"context"
    22  
    23  	"go.uber.org/zap"
    24  
    25  	"github.com/polarismesh/polaris/common/model"
    26  	"github.com/polarismesh/polaris/common/utils"
    27  	"github.com/polarismesh/polaris/service"
    28  	"github.com/polarismesh/polaris/store"
    29  )
    30  
    31  type (
    32  	sourceFromEureka struct{}
    33  )
    34  
    35  func (h *EurekaServer) registerInstanceChain() {
    36  	svr := h.originDiscoverSvr.(*service.Server)
    37  	svr.AddInstanceChain(&EurekaInstanceChain{
    38  		s: h.namingServer.Cache().GetStore(),
    39  	})
    40  }
    41  
    42  type EurekaInstanceChain struct {
    43  	s store.Store
    44  }
    45  
    46  func (c *EurekaInstanceChain) AfterUpdate(ctx context.Context, instances ...*model.Instance) {
    47  	isFromEureka, _ := ctx.Value(sourceFromEureka{}).(bool)
    48  	if isFromEureka {
    49  		return
    50  	}
    51  
    52  	// TODO:这里要注意避免 eureka -> polaris -> notify -> eureka 带来的重复操作,后续会在 context 中携带信息做判断处理
    53  	for i := range instances {
    54  		ins := instances[i]
    55  		metadata := ins.Proto.GetMetadata()
    56  		if _, ok := metadata[InternalMetadataStatus]; !ok {
    57  			continue
    58  		}
    59  		if ins.Isolate() {
    60  			metadata[InternalMetadataStatus] = StatusOutOfService
    61  		} else {
    62  			metadata[InternalMetadataStatus] = StatusUp
    63  		}
    64  		if err := c.s.BatchAppendInstanceMetadata([]*store.InstanceMetadataRequest{
    65  			{
    66  				InstanceID: ins.ID(),
    67  				Revision:   utils.NewUUID(),
    68  				Metadata: map[string]string{
    69  					InternalMetadataStatus: metadata[InternalMetadataStatus],
    70  				},
    71  			},
    72  		}); err != nil {
    73  			eurekalog.Error("[EUREKA-SERVER] after update instance isolate fail", zap.Error(err))
    74  		}
    75  	}
    76  }