Welcome to mirror list, hosted at ThFree Co, Russian Federation.

subcmd_reconcile.go « praefect « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 20734b48dc8894c701de0290b643b5d88383e71e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main

import (
	"bytes"
	"context"
	"errors"
	"flag"
	"fmt"
	"io"
	"log"

	"gitlab.com/gitlab-org/gitaly/internal/praefect/config"
	"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)

type nodeReconciler struct {
	conf                  config.Config
	virtualStorage        string
	targetStorage         string
	referenceStorage      string
	disableReconciliation bool
}

type reconcileSubcommand struct {
	virtual   string
	target    string
	reference string
	force     bool
}

func (s *reconcileSubcommand) FlagSet() *flag.FlagSet {
	fs := flag.NewFlagSet("reconcile", flag.ExitOnError)
	fs.StringVar(&s.virtual, "virtual", "", "virtual storage for target storage")
	fs.StringVar(&s.target, "target", "", "target storage to reconcile")
	fs.StringVar(&s.reference, "reference", "", "reference storage to reconcile (optional)")
	fs.BoolVar(&s.force, "f", false, "actually schedule replications")
	return fs
}

func (s *reconcileSubcommand) Exec(flags *flag.FlagSet, conf config.Config) error {
	logger.Warn("The reconcile sub-command has been deprecated in GitLab 13.12 and is scheduled for removal in GitLab 14.0. Use the automatic reconciler instead: https://docs.gitlab.com/ee/administration/gitaly/praefect.html#automatic-reconciliation")

	nr := nodeReconciler{
		conf:                  conf,
		virtualStorage:        s.virtual,
		targetStorage:         s.target,
		referenceStorage:      s.reference,
		disableReconciliation: !s.force,
	}

	if err := nr.reconcile(); err != nil {
		return fmt.Errorf("unable to reconcile: %s", err)
	}

	return nil
}

func getNodeAddress(cfg config.Config) (string, error) {
	switch {
	case cfg.SocketPath != "":
		return "unix://" + cfg.SocketPath, nil
	case cfg.ListenAddr != "":
		return "tcp://" + cfg.ListenAddr, nil
	default:
		return "", errors.New("no Praefect address configured")
	}
}

func (nr nodeReconciler) reconcile() error {
	if err := nr.validateArgs(); err != nil {
		return err
	}

	nodeAddr, err := getNodeAddress(nr.conf)
	if err != nil {
		return err
	}

	cc, err := subCmdDial(nodeAddr, nr.conf.Auth.Token)
	if err != nil {
		return err
	}

	pCli := gitalypb.NewPraefectInfoServiceClient(cc)

	if nr.disableReconciliation {
		log.Print("Performing a DRY RUN - no changes will be made until '-f' flag is provided")
	} else {
		log.Print("Performing a LIVE RUN - any repositories on target that are inconsistent with reference will be overwritten with the version present on reference")
	}

	request := &gitalypb.ConsistencyCheckRequest{
		VirtualStorage:         nr.virtualStorage,
		TargetStorage:          nr.targetStorage,
		ReferenceStorage:       nr.referenceStorage,
		DisableReconcilliation: nr.disableReconciliation,
	}
	stream, err := pCli.ConsistencyCheck(context.TODO(), request)
	if err != nil {
		return err
	}

	log.Print("Checking consistency...")
	if err := nr.consumeStream(stream); err != nil {
		return err
	}

	return nil
}

func (nr nodeReconciler) validateArgs() error {
	var vsFound, tFound, rFound bool

	for _, vs := range nr.conf.VirtualStorages {
		if vs.Name != nr.virtualStorage {
			continue
		}
		vsFound = true

		for _, n := range vs.Nodes {
			if n.Storage == nr.targetStorage {
				tFound = true
			}
			if n.Storage == nr.referenceStorage {
				rFound = true
			}
		}
	}

	if !vsFound {
		return fmt.Errorf(
			"cannot find virtual storage %s in config", nr.virtualStorage,
		)
	}
	if !tFound {
		return fmt.Errorf(
			"cannot find target storage %s in virtual storage %q in config",
			nr.targetStorage, nr.virtualStorage,
		)
	}
	if nr.referenceStorage != "" && !rFound {
		return fmt.Errorf(
			"cannot find reference storage %q in virtual storage %q in config",
			nr.referenceStorage, nr.virtualStorage,
		)
	}

	return nil
}

func (nr nodeReconciler) consumeStream(stream gitalypb.PraefectInfoService_ConsistencyCheckClient) error {
	var rStorage string
	var i uint

	for ; ; i++ {
		resp, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		if resp.ReferenceStorage != rStorage {
			rStorage = resp.ReferenceStorage
			log.Print("Reference storage being used: " + rStorage)
		}

		if len(resp.Errors) > 0 {
			var composedErrMsg bytes.Buffer
			for _, errMsg := range resp.Errors {
				composedErrMsg.WriteString("\t")
				composedErrMsg.WriteString(errMsg)
				composedErrMsg.WriteString("\n")
			}
			log.Printf("FAILURE: Internal error(s) occurred for the repo %s: %s", resp.GetRepoRelativePath(), composedErrMsg.String())
			continue
		}

		if resp.GetReferenceChecksum() == resp.GetTargetChecksum() {
			log.Print("CONSISTENT: " + resp.GetRepoRelativePath())
			continue
		}

		checksumPrint := func(checksum string) string {
			if checksum == "" {
				return "null"
			}
			return checksum
		}

		log.Printf(
			"INCONSISTENT: Repo %s has checksum %s on target but checksum %s on reference storage %s",
			resp.GetRepoRelativePath(),
			checksumPrint(resp.GetTargetChecksum()),
			checksumPrint(resp.GetReferenceChecksum()),
			resp.GetReferenceStorage(),
		)
		if resp.GetReplJobId() != 0 {
			log.Printf(
				"SCHEDULED: Replication job %d will update repo %s",
				resp.GetReplJobId(),
				resp.GetRepoRelativePath(),
			)
		}
	}

	log.Printf("FINISHED: %d repos were checked for consistency", i)
	return nil
}