summaryrefslogtreecommitdiff
path: root/ndb/home/bin/ngcalc
blob: a289d384db96c02153083dcced7e3e9253d07124 (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
#! /usr/local/bin/perl

use strict;
use Getopt::Long;

sub usage {
    print <<END;
ngcalc -- calculate node groups and table fragments
usage: ngcalc [ options ] f1 f2 ...
-g num	number of node groups (default 2)
-r num	number of replicas (default 2)
-n list	comma-separated list of db nodes (default 1,2,...)
fX	number of fragments per node group in table X (e.g. 1,2,8)
        (all replicas count as same fragment)
END
    exit(1);
};

use vars qw($cnoOfNodeGroups $cnoReplicas $nodeArray);

$cnoOfNodeGroups = 2;
$cnoReplicas = 2;
GetOptions(
    "g=i" => \$cnoOfNodeGroups,
    "r=i" => \$cnoReplicas,
    "n=s" => \$nodeArray,
) or &usage;

my @tableList = @ARGV;

$cnoOfNodeGroups > 0 or &usage;
$cnoReplicas > 0 or &usage;
if (! defined($nodeArray)) {
    $nodeArray = join(',', 1..($cnoOfNodeGroups*$cnoReplicas));
}
$nodeArray =~ /^\d+(,\d+)*$/ or &usage;
my @nodeArray = split(/,/, $nodeArray);
@nodeArray == $cnoOfNodeGroups*$cnoReplicas or &usage;

my @nodeGroupRecord;
for (my $i = 0; $i < $cnoOfNodeGroups; $i++) {
    my $rec = {};
    my $nodes = [];
    for (my $j = 0; $j < $cnoReplicas; $j++) {
	push(@$nodes, $nodeArray[$i * $cnoReplicas + $j]);
    }
    $rec->{nodesInGroup} = $nodes;
    $rec->{nodeCount} = $cnoReplicas;
    $rec->{nextReplicaNode} = 0;
    $nodeGroupRecord[$i] = $rec;
    print "NG $i: ", join(" ", @{$rec->{nodesInGroup}}), "\n";
}

# see Dbdih::execCREATE_FRAGMENTATION_REQ

my $c_nextNodeGroup = 0;
for (my $t = 0; $t < @tableList; $t++) {
    use integer;
    my $f = $tableList[$t];
    my $ng = $c_nextNodeGroup++;
    $c_nextNodeGroup = 0 if $c_nextNodeGroup == $cnoOfNodeGroups;
    my $noOfFragments = $f * $cnoOfNodeGroups;
    my @fragments;
    for (my $fragNo = 0; $fragNo < $noOfFragments; $fragNo++) {
	my $rec = $nodeGroupRecord[$ng];
	my $max = $rec->{nodeCount};
	my $ind = $rec->{nextReplicaNode};
	$rec->{nextReplicaNode} = ($ind + 1 >= $max ? 0 : $ind + 1);
	for (my $replicaNo = 0; $replicaNo < $cnoReplicas; $replicaNo++) {
	    my $nodeId = $rec->{nodesInGroup}[$ind++];
	    push(@fragments, $nodeId);
	    $ind = ($ind == $max ? 0 : $ind);
	}
	$ng++;
	$ng = ($ng == $cnoOfNodeGroups ? 0 : $ng);
    }
    printf "%02d %s\n", $t, join(" ", @fragments);
}