blob: 20477a73741e9698cb2176e6e360d82d3a3a88d3 [file] [log] [blame]
/*
* 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 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.
*/
package server
import (
"time"
)
import (
"github.com/apache/dubbo-kubernetes/pkg/core"
util_xds "github.com/apache/dubbo-kubernetes/pkg/util/xds"
)
var nackLog = core.Log.WithName("dds-delta").WithName("nack-backoff")
type nackBackoff struct {
backoff time.Duration
util_xds.NoopCallbacks
}
var _ util_xds.DeltaCallbacks = &nackBackoff{}
func NewNackBackoff(backoff time.Duration) util_xds.DeltaCallbacks {
return &nackBackoff{
backoff: backoff,
}
}
func (n *nackBackoff) OnStreamDeltaResponse(_ int64, request util_xds.DeltaDiscoveryRequest, _ util_xds.DeltaDiscoveryResponse) {
if request.HasErrors() {
// When DiscoveryRequest contains errors, it means that a control plane rejected configuration generated by the other control plane
// It may happen for several reasons:
// 1) Eventual consistency - ex. MeshTrafficPermission, but Mesh for this TrafficPermission is not synced yet.
// 2) Config is valid from one control plane side but invalid from the other side - ex. schema is broken
//
// Second case is especially dangerous because we will end up in a loop.
// CP is constantly trying to send a config and other cp immediately rejects the config.
// Without this backoff, CP is under a lot of pressure from faulty control plane.
//
// It is safe to sleep here because OnStreamResponse is executed in the goroutine of a single ADS stream
nackLog.Info("config was previously rejected by other control plane. Applying backoff before resending it", "backoff", n.backoff, "nodeID", request.NodeId(), "reason", request.ErrorMsg())
time.Sleep(n.backoff)
}
}