sync_raft_restore.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "sync_raft_config_change.h"
#include "sync_raft_restore.h"
#include "sync_raft_progress_tracker.h"

20
static void addToConfChangeSingleArray(SSyncConfChangeSingleArray* out, int* i, const SSyncRaftNodeMap* nodeMap, ESyncRaftConfChangeType t);
21 22
static int toConfChangeSingle(const SSyncConfigState* cs, SSyncConfChangeSingleArray* out, SSyncConfChangeSingleArray* in);

23
// syncRaftRestoreConfig takes a Changer (which must represent an empty configuration), and
24 25 26 27 28 29 30
// runs a sequence of changes enacting the configuration described in the
// ConfState.
//
// TODO(tbg) it's silly that this takes a Changer. Unravel this by making sure
// the Changer only needs a ProgressMap (not a whole Tracker) at which point
// this can just take LastIndex and MaxInflight directly instead and cook up
// the results from that alone.
31 32
int syncRaftRestoreConfig(SSyncRaftChanger* changer, const SSyncConfigState* cs,
                          SSyncRaftProgressTrackerConfig* config, SSyncRaftProgressMap* progressMap) {
33 34 35
  SSyncConfChangeSingleArray outgoing;
  SSyncConfChangeSingleArray incoming;
  SSyncConfChangeSingleArray css;
36
  SSyncRaftProgressTracker* tracker = changer->tracker;
37 38
  int i, ret;

39 40 41 42 43 44
  syncRaftInitConfArray(&outgoing);
  syncRaftInitConfArray(&incoming);

  syncRaftInitTrackConfig(config);
  syncRaftInitProgressMap(progressMap);
  
45 46 47 48 49
  ret = toConfChangeSingle(cs, &outgoing, &incoming);
  if (ret != 0) {
    goto out;
  }

50
  if (syncRaftConfArrayIsEmpty(&outgoing)) {
51 52 53 54 55 56
    // No outgoing config, so just apply the incoming changes one by one.
    for (i = 0; i < incoming.n; ++i) {
      css = (SSyncConfChangeSingleArray) {
        .n = 1,
        .changes = &incoming.changes[i],
      };
57
      ret = syncRaftChangerSimpleConfig(changer, &css, config, progressMap);
58 59 60
      if (ret != 0) {
        goto out;
      }
61 62 63

      syncRaftCopyTrackerConfig(config, &changer->tracker->config);
      syncRaftCopyProgressMap(progressMap, &changer->tracker->progressMap);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    }
  } else {
		// The ConfState describes a joint configuration.
		//
		// First, apply all of the changes of the outgoing config one by one, so
		// that it temporarily becomes the incoming active config. For example,
		// if the config is (1 2 3)&(2 3 4), this will establish (2 3 4)&().
    for (i = 0; i < outgoing.n; ++i) {
      css = (SSyncConfChangeSingleArray) {
        .n = 1,
        .changes = &outgoing.changes[i],
      };
      ret = syncRaftChangerSimpleConfig(changer, &css, config, progressMap);
      if (ret != 0) {
        goto out;
      }
80 81
      syncRaftCopyTrackerConfig(config, &changer->tracker->config);
      syncRaftCopyProgressMap(progressMap, &changer->tracker->progressMap);
82 83
    }

84
    ret = syncRaftChangerEnterJoint(changer, cs->autoLeave, &incoming, config, progressMap);
85 86 87 88 89 90
    if (ret != 0) {
      goto out;
    }
  }

out:
91 92 93
  syncRaftFreeConfArray(&incoming);
  syncRaftFreeConfArray(&outgoing);

94 95 96
  return ret;
}

97 98 99 100 101 102 103 104 105 106 107 108
static void addToConfChangeSingleArray(SSyncConfChangeSingleArray* out, int* i, const SSyncRaftNodeMap* nodeMap, ESyncRaftConfChangeType t) {
  SyncNodeId* pId = NULL;

  while (!syncRaftIterateNodeMap(nodeMap, pId)) {
    out->changes[*i] = (SSyncConfChangeSingle) {
      .type = t,
      .nodeId = *pId,
    };
    *i += 1;
  }
}

109 110 111 112 113 114 115
// toConfChangeSingle translates a conf state into 1) a slice of operations creating
// first the config that will become the outgoing one, and then the incoming one, and
// b) another slice that, when applied to the config resulted from 1), represents the
// ConfState.
static int toConfChangeSingle(const SSyncConfigState* cs, SSyncConfChangeSingleArray* out, SSyncConfChangeSingleArray* in) {
  int i;

116
  out->n = syncRaftNodeMapSize(&cs->votersOutgoing);
117 118 119 120 121
  out->changes = (SSyncConfChangeSingle*)malloc(sizeof(SSyncConfChangeSingle) * out->n);
  if (out->changes == NULL) {
    out->n = 0;
    return -1;
  }
122 123 124 125
  in->n = syncRaftNodeMapSize(&cs->votersOutgoing) + 
          syncRaftNodeMapSize(&cs->voters) + 
          syncRaftNodeMapSize(&cs->learners) + 
          syncRaftNodeMapSize(&cs->learnersNext);
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
  out->changes = (SSyncConfChangeSingle*)malloc(sizeof(SSyncConfChangeSingle) * in->n);
  if (in->changes == NULL) {
    in->n = 0;
    return -1;
  }

	// Example to follow along this code:
	// voters=(1 2 3) learners=(5) outgoing=(1 2 4 6) learners_next=(4)
	//
	// This means that before entering the joint config, the configuration
	// had voters (1 2 4 6) and perhaps some learners that are already gone.
	// The new set of voters is (1 2 3), i.e. (1 2) were kept around, and (4 6)
	// are no longer voters; however 4 is poised to become a learner upon leaving
	// the joint state.
	// We can't tell whether 5 was a learner before entering the joint config,
	// but it doesn't matter (we'll pretend that it wasn't).
	//
	// The code below will construct
	// outgoing = add 1; add 2; add 4; add 6
	// incoming = remove 1; remove 2; remove 4; remove 6
	//            add 1;    add 2;    add 3;
	//            add-learner 5;
	//            add-learner 4;
	//
	// So, when starting with an empty config, after applying 'outgoing' we have
	//
	//   quorum=(1 2 4 6)
	//
	// From which we enter a joint state via 'incoming'
	//
	//   quorum=(1 2 3)&&(1 2 4 6) learners=(5) learners_next=(4)
	//
	// as desired.

160 161 162 163 164
  // If there are outgoing voters, first add them one by one so that the
	// (non-joint) config has them all.
  i = 0;
  addToConfChangeSingleArray(out, &i, &cs->votersOutgoing, SYNC_RAFT_Conf_AddNode);
  assert(i == out->n);
165 166 167

	// We're done constructing the outgoing slice, now on to the incoming one
	// (which will apply on top of the config created by the outgoing slice).
168 169
  i = 0;
  
170
	// First, we'll remove all of the outgoing voters.
171 172
  addToConfChangeSingleArray(in, &i, &cs->votersOutgoing, SYNC_RAFT_Conf_RemoveNode);

173
  // Then we'll add the incoming voters and learners.
174 175 176 177 178
  addToConfChangeSingleArray(in, &i, &cs->voters, SYNC_RAFT_Conf_AddNode);
  addToConfChangeSingleArray(in, &i, &cs->learners, SYNC_RAFT_Conf_AddLearnerNode);
  addToConfChangeSingleArray(in, &i, &cs->learnersNext, SYNC_RAFT_Conf_AddLearnerNode);
  assert(i == in->n);

179 180
  return 0;
}