From ca0a33ea0febe0a44f5ca70a61c92a23b6cbd4d5 Mon Sep 17 00:00:00 2001
From: Evgenii Stratonikov <e.stratonikov@yadro.com>
Date: Wed, 25 Dec 2024 12:04:08 +0300
Subject: [PATCH] [#465] objsvc: Set NETMAP_EPOCH xheader for auxiliary
 requests

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
---
 pkg/services/object/internal/client/client.go | 28 +++++++++++++------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/pkg/services/object/internal/client/client.go b/pkg/services/object/internal/client/client.go
index 2c405070d..3e8832640 100644
--- a/pkg/services/object/internal/client/client.go
+++ b/pkg/services/object/internal/client/client.go
@@ -7,9 +7,11 @@ import (
 	"errors"
 	"fmt"
 	"io"
+	"strconv"
 
 	coreclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
 	"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
+	sessionAPI "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
 	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
 	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
 	apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
@@ -31,6 +33,8 @@ type commonPrm struct {
 	local bool
 
 	xHeaders []string
+
+	netmapEpoch uint64
 }
 
 // SetClient sets base client for ForstFS API communication.
@@ -73,6 +77,14 @@ func (x *commonPrm) SetXHeaders(hs []string) {
 	x.xHeaders = hs
 }
 
+func (x *commonPrm) calculateXHeaders() []string {
+	hs := x.xHeaders
+	if x.netmapEpoch != 0 {
+		hs = append(hs, sessionAPI.XHeaderNetmapEpoch, strconv.FormatUint(x.netmapEpoch, 10))
+	}
+	return hs
+}
+
 type readPrmCommon struct {
 	commonPrm
 }
@@ -80,8 +92,8 @@ type readPrmCommon struct {
 // SetNetmapEpoch sets the epoch number to be used to locate the objectSDK.
 //
 // By default current epoch on the server will be used.
-func (x *readPrmCommon) SetNetmapEpoch(_ uint64) {
-	// FIXME(@fyrchik): https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/465
+func (x *readPrmCommon) SetNetmapEpoch(epoch uint64) {
+	x.netmapEpoch = epoch
 }
 
 // GetObjectPrm groups parameters of GetObject operation.
@@ -139,7 +151,7 @@ func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) {
 		prm.ClientParams.Session = prm.tokenSession
 	}
 
-	prm.ClientParams.XHeaders = prm.xHeaders
+	prm.ClientParams.XHeaders = prm.calculateXHeaders()
 	prm.ClientParams.BearerToken = prm.tokenBearer
 	prm.ClientParams.Local = prm.local
 	prm.ClientParams.Key = prm.key
@@ -233,7 +245,7 @@ func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error)
 
 	prm.ClientParams.BearerToken = prm.tokenBearer
 	prm.ClientParams.Local = prm.local
-	prm.ClientParams.XHeaders = prm.xHeaders
+	prm.ClientParams.XHeaders = prm.calculateXHeaders()
 
 	cliRes, err := prm.cli.ObjectHead(ctx, prm.ClientParams)
 	if err == nil {
@@ -326,7 +338,7 @@ func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, e
 		prm.ClientParams.Session = prm.tokenSession
 	}
 
-	prm.ClientParams.XHeaders = prm.xHeaders
+	prm.ClientParams.XHeaders = prm.calculateXHeaders()
 	prm.ClientParams.BearerToken = prm.tokenBearer
 	prm.ClientParams.Local = prm.local
 	prm.ClientParams.Length = prm.ln
@@ -390,7 +402,7 @@ func PutObject(ctx context.Context, prm PutObjectPrm) (*PutObjectRes, error) {
 	defer span.End()
 
 	prmCli := client.PrmObjectPutInit{
-		XHeaders:    prm.xHeaders,
+		XHeaders:    prm.calculateXHeaders(),
 		BearerToken: prm.tokenBearer,
 		Session:     prm.tokenSession,
 		Local:       true,
@@ -437,7 +449,7 @@ func PutObjectSingle(ctx context.Context, prm PutObjectPrm) (*PutObjectRes, erro
 	}
 
 	prmCli := client.PrmObjectPutSingle{
-		XHeaders:    prm.xHeaders,
+		XHeaders:    prm.calculateXHeaders(),
 		BearerToken: prm.tokenBearer,
 		Session:     prm.tokenSession,
 		Local:       true,
@@ -496,7 +508,7 @@ func SearchObjects(ctx context.Context, prm SearchObjectsPrm) (*SearchObjectsRes
 	prm.cliPrm.Local = prm.local
 	prm.cliPrm.Session = prm.tokenSession
 	prm.cliPrm.BearerToken = prm.tokenBearer
-	prm.cliPrm.XHeaders = prm.xHeaders
+	prm.cliPrm.XHeaders = prm.calculateXHeaders()
 	prm.cliPrm.Key = prm.key
 
 	rdr, err := prm.cli.ObjectSearchInit(ctx, prm.cliPrm)