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

subcmd_dataloss.go « praefect « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9c7f2d22be21edba4b3e2ee32355f4d8d94bed1 (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
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"sort"
	"strings"

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

type datalossSubcommand struct {
	output         io.Writer
	virtualStorage string
}

func newDatalossSubcommand() *datalossSubcommand {
	return &datalossSubcommand{output: os.Stdout}
}

func (cmd *datalossSubcommand) FlagSet() *flag.FlagSet {
	fs := flag.NewFlagSet("dataloss", flag.ContinueOnError)
	fs.StringVar(&cmd.virtualStorage, "virtual-storage", "", "virtual storage to check for data loss")
	return fs
}

func (cmd *datalossSubcommand) println(indent int, msg string, args ...interface{}) {
	fmt.Fprint(cmd.output, strings.Repeat("  ", indent))
	fmt.Fprintf(cmd.output, msg, args...)
	fmt.Fprint(cmd.output, "\n")
}

func (cmd *datalossSubcommand) Exec(flags *flag.FlagSet, cfg config.Config) error {
	if flags.NArg() > 0 {
		return UnexpectedPositionalArgsError{Command: flags.Name()}
	}

	virtualStorages := []string{cmd.virtualStorage}
	if cmd.virtualStorage == "" {
		virtualStorages = make([]string, len(cfg.VirtualStorages))
		for i := range cfg.VirtualStorages {
			virtualStorages[i] = cfg.VirtualStorages[i].Name
		}
	}
	sort.Strings(virtualStorages)

	nodeAddr, err := getNodeAddress(cfg)
	if err != nil {
		return err
	}

	conn, err := subCmdDial(nodeAddr, cfg.Auth.Token)
	if err != nil {
		return fmt.Errorf("error dialing: %v", err)
	}
	defer func() {
		if err := conn.Close(); err != nil {
			log.Printf("error closing connection: %v", err)
		}
	}()

	client := gitalypb.NewPraefectInfoServiceClient(conn)

	for _, vs := range virtualStorages {
		resp, err := client.DatalossCheck(context.Background(), &gitalypb.DatalossCheckRequest{
			VirtualStorage: vs,
		})
		if err != nil {
			return fmt.Errorf("error checking: %v", err)
		}

		mode := "write-enabled"
		if resp.IsReadOnly {
			mode = "read-only"
		}

		cmd.println(0, "Virtual storage: %s", vs)
		cmd.println(1, "Current %s primary: %s", mode, resp.CurrentPrimary)
		if resp.PreviousWritablePrimary == "" {
			fmt.Fprintln(cmd.output, "    No data loss as the virtual storage has not encountered a failover")
			continue
		}

		cmd.println(1, "Previous write-enabled primary: %s", resp.PreviousWritablePrimary)
		if len(resp.OutdatedNodes) == 0 {
			cmd.println(2, "No data loss from failing over from %s", resp.PreviousWritablePrimary)
			continue
		}

		cmd.println(2, "Nodes with data loss from failing over from %s:", resp.PreviousWritablePrimary)
		for _, odn := range resp.OutdatedNodes {
			cmd.println(3, "%s: %s", odn.RelativePath, strings.Join(odn.Nodes, ", "))
		}
	}

	return nil
}