0z5a opened a new issue, #3817:
URL: https://github.com/apache/dubbo-go/issues/3817

   ### What happened?
   
   A provider that has just been exported gets its full weight on the consumer 
side right away, so the warmup ramp never happens. With interface-level 
registration, a provider exported 60 seconds ago with `warmup=600` should get 
weight 100 * 60 / 600 = 10, but `loadbalance.GetWeight` returns 100 and the 
random load balancer sends it about half of the traffic instead of about 9%.
   
   Expected: weight 10 for the fresh provider (about 10/110 = 9% of calls 
against a long-running provider with weight 100).
   Actual: weight 100 (about 50% of calls).
   Error: none, the warmup setting is silently ignored.
   
   `GetWeightAt` in `cluster/loadbalance/util.go` reads the provider start time 
from `remote.timestamp`. The only place that writes this key is `URL.MergeURL` 
in `common/url.go`, which the registry directory calls as 
`providerURL.MergeURL(referenceURL)`. There the condition is inverted:
   
   ```go
   baseTimestamp := params.Get(constant.TimestampKey)
   baseHasTimestamp := baseTimestamp != ""
   ...
   if !baseHasTimestamp {
        params[constant.RemoteTimestampKey] = []string{baseTimestamp}
        params[constant.TimestampKey] = 
[]string{anotherUrl.params.Get(constant.TimestampKey)}
   }
   ```
   
   When the provider URL has a `timestamp`, `remote.timestamp` is never 
written. With interface-level registration it does have one: `server/action.go` 
sets it at export, and `registry/protocol` keeps it in both the normal and the 
`simplified` registry URL. When it has none, `remote.timestamp` is set to an 
empty string. In both cases `GetWeightAt` falls back to `now`, uptime is 0 and 
the weight is never reduced.
   
   The original code from 2019 was `if v := 
serviceUrl.Params.Get(constant.TIMESTAMP_KEY); v != "" { 
mergedUrl.Params.Set(constant.REMOTE_TIMESTAMP_KEY, v) ... }`. In #2022 (in 
releases since v3.1.0) it became `if v, ok := 
serviceURL.GetParamNoDefault(constant.TimestampKey); !ok`, which flipped the 
meaning. `TestRandomlbSelectWarmup` did not catch it because it sets 
`remote.timestamp` on the URL directly instead of going through `MergeURL`.
   
   For reference, Java Dubbo takes the warmup start from the provider's export 
time in both lines. In 2.6.x, `ClusterUtils.mergeUrl` reserves the provider 
`timestamp` as `remote.timestamp`, and `AbstractLoadBalance.getWeight` reads 
`remote.timestamp`. In 3.3.x, `DefaultProviderURLMergeProcessor` removes the 
consumer `timestamp` so the provider's value is kept, and 
`AbstractLoadBalance.getWeight` reads `timestamp`.
   
   ### How can we reproduce it? (optional)
   
   Put this file in `cluster/loadbalance/random/` and run `go test 
./cluster/loadbalance/random/ -run TestWarmupRepro -v -count=1`:
   
   ```go
   package random
   
   import (
        "fmt"
        "strconv"
        "testing"
        "time"
   )
   
   import (
        "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
        "dubbo.apache.org/dubbo-go/v3/common"
        "dubbo.apache.org/dubbo-go/v3/protocol/base"
        "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
   )
   
   func TestWarmupRepro(t *testing.T) {
        now := time.Now().Unix()
        inv := 
invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("SayHello"))
        ref, _ := 
common.NewURL(fmt.Sprintf("tri://127.0.0.1/com.example.Greeter?side=consumer&timestamp=%d",
 now-3600))
        // provider started 60 s ago with warmup=600 s, and a provider that has 
been up for a day
        fresh, _ := 
common.NewURL(fmt.Sprintf("tri://10.0.0.1:20000/com.example.Greeter?side=provider&warmup=600&timestamp=%d",
 now-60))
        old, _ := 
common.NewURL(fmt.Sprintf("tri://10.0.0.2:20000/com.example.Greeter?side=provider&warmup=600&timestamp=%d",
 now-86400))
   
        // same merge order as registry/directory: 
providerURL.MergeURL(referenceURL)
        mFresh, mOld := fresh.MergeURL(ref), old.MergeURL(ref)
        fmt.Printf("merged: timestamp=%q remote.timestamp=%q\n", 
mFresh.GetParam("timestamp", ""), mFresh.GetParam("remote.timestamp", ""))
        fmt.Printf("weight fresh=%d old=%d\n",
                loadbalance.GetWeightAt(base.NewBaseInvoker(mFresh), inv, now), 
loadbalance.GetWeightAt(base.NewBaseInvoker(mOld), inv, now))
   
        // control: the same URL with remote.timestamp set by hand
        ctl := mFresh.Clone()
        ctl.SetParam("remote.timestamp", strconv.FormatInt(now-60, 10))
        fmt.Printf("control weight fresh=%d\n", 
loadbalance.GetWeightAt(base.NewBaseInvoker(ctl), inv, now))
   
        lb, hits := NewRandomLoadBalance(), 0
        invokers := []base.Invoker{base.NewBaseInvoker(mFresh), 
base.NewBaseInvoker(mOld)}
        for range 20000 {
                if lb.Select(invokers, inv).GetURL().Ip == "10.0.0.1" {
                        hits++
                }
        }
        fmt.Printf("random LB: fresh provider got %.1f%% of calls\n", 
float64(hits)/200)
   }
   ```
   
   Output on main:
   
   ```
   merged: timestamp="1794753991" remote.timestamp=""
   weight fresh=100 old=100
   control weight fresh=10
   random LB: fresh provider got 50.2% of calls
   ```
   
   The "control" line shows that the warmup math itself works once 
`remote.timestamp` is present. I also checked the full consumer path with the 
mock registry from the `registry/directory` tests (registry add event -> 
`RegistryDirectory` -> cached invoker URL -> `GetWeightAt`). The invoker URL 
has the provider's `timestamp` but no `remote.timestamp`, and the weight is 100 
instead of 10.
   
   Proposed fix: in `MergeURL`, when the base (provider) URL has a `timestamp`, 
copy it to `remote.timestamp` and leave `timestamp` as it is. `timestamp` 
should stay the provider's value because the closing-tombstone check in 
`registry/directory` compares it to detect a real restart. With that change the 
repro prints weight 10 and about 9% of calls, and `go test ./common/... 
./registry/directory/... ./cluster/... ./client/...` passes.
   
   Two notes on scope. `server/action.go` always sets `warmup`, and an empty 
value falls back to `DefaultWarmup` (600 s), so after the fix Go providers 
found through interface-level registries get the default 10-minute ramp, the 
same default as Java. Providers that register a millisecond timestamp (Java 
providers) are not affected: uptime comes out negative and the weight stays as 
it is today. Application-level service discovery does not put `timestamp` into 
the provider URL, so it is not covered by this change.
   
   Related but separate: `WithWarmup` / `WithServerWarmup` store 
`time.Duration.String()` values such as `30s`, and `GetWeightAt` reads `warmup` 
with `GetParamInt`, so those values fall back to 600 s. I can file that 
separately if it helps.
   
   ### Environment
   
   - dubbo-go: main at d79ad5d82356dd8167f710ab91c7081ef49afdb5 (same code in 
v3.3.2; present since v3.1.0)
   - Go: 1.27.1
   - OS: macOS 26 (arm64)
   - Configuration: interface-level registry (provider URLs carry `timestamp`), 
default load balancer settings, `warmup` unset or set in seconds
   
   I'm happy to send a PR with the fix and a regression test if that works for 
you.
   
   This issue was drafted with Claude Code (claude-opus-5-5); the results above 
are from the runs described.
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to