Skip to content

Commit 494e6dc

Browse files
committed
2 parents 7173128 + 81087da commit 494e6dc

File tree

4 files changed

+264
-3
lines changed

4 files changed

+264
-3
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package RemoteCluster;
2+
3+
use strict;
4+
use warnings;
5+
use Data::Dumper;
6+
use Net::OpenSSH;
7+
use Cwd;
8+
9+
sub new
10+
{
11+
my ($class, $config_fname) = @_;
12+
open(my $config, '<', $config_fname);
13+
my @config_lines = <$config>;
14+
my @nodes = ();
15+
16+
# Parse connection options from ssh_config
17+
my $node;
18+
foreach (@config_lines)
19+
{
20+
if (/^Host (.+)/)
21+
{
22+
if ($node->{'host'}){
23+
push(@nodes, $node);
24+
$node = {};
25+
}
26+
$node->{'host'} = $1;
27+
}
28+
elsif (/\s*([^\s]+)\s*([^\s]+)\s*/)
29+
{
30+
$node->{'cfg'}->{$1} = $2;
31+
}
32+
}
33+
push(@nodes, $node);
34+
35+
# print Dumper(@nodes);
36+
37+
my $self = {
38+
nnodes => scalar @nodes,
39+
nodes => \@nodes,
40+
};
41+
42+
bless $self, $class;
43+
return $self;
44+
}
45+
46+
sub run
47+
{
48+
my ($self, $node_id, $cmd) = @_;
49+
my $node = $self->{nodes}[$node_id];
50+
my $opts = $node->{cfg};
51+
52+
print "===\n";
53+
print Dumper($opts);
54+
print "===\n";
55+
56+
my $ssh = Net::OpenSSH->new(
57+
$opts->{HostName},
58+
port => $opts->{Port},
59+
user => $opts->{User},
60+
key_path => $opts->{IdentityFile} =~ /"([^"]*)"/,
61+
master_opts => [-o => "StrictHostKeyChecking=no"]
62+
);
63+
64+
my @ls = $ssh->capture($cmd);
65+
66+
print Dumper(@ls);
67+
68+
}
69+
70+
sub init
71+
{
72+
my ($self) = @_;
73+
my $nodes = $self->{nodes};
74+
75+
foreach my $node (@$nodes)
76+
{
77+
$node->init(hba_permit_replication => 0);
78+
}
79+
}
80+
81+
sub detach
82+
{
83+
my ($self) = @_;
84+
my $nodes = $self->{nodes};
85+
86+
foreach my $node (@$nodes)
87+
{
88+
delete $node->{_pid};
89+
}
90+
}
91+
92+
sub configure
93+
{
94+
my ($self) = @_;
95+
my $nodes = $self->{nodes};
96+
97+
my $connstr = join(',', map { "${ \$_->connstr('postgres') }" } @$nodes);
98+
my $raftpeers = join(',', map { join(':', $_->{id}, $_->host, $_->{raftport}) } @$nodes);
99+
100+
foreach my $node (@$nodes)
101+
{
102+
my $id = $node->{id};
103+
my $host = $node->host;
104+
my $pgport = $node->port;
105+
my $raftport = $node->{raftport};
106+
107+
$node->append_conf("postgresql.conf", qq(
108+
listen_addresses = '$host'
109+
unix_socket_directories = ''
110+
port = $pgport
111+
max_prepared_transactions = 200
112+
max_connections = 200
113+
max_worker_processes = 100
114+
wal_level = logical
115+
fsync = off
116+
max_wal_senders = 10
117+
wal_sender_timeout = 0
118+
max_replication_slots = 10
119+
shared_preload_libraries = 'raftable,multimaster'
120+
multimaster.workers = 10
121+
multimaster.queue_size = 10485760 # 10mb
122+
multimaster.node_id = $id
123+
multimaster.conn_strings = '$connstr'
124+
multimaster.use_raftable = true
125+
multimaster.ignore_tables_without_pk = true
126+
multimaster.twopc_min_timeout = 60000
127+
raftable.id = $id
128+
raftable.peers = '$raftpeers'
129+
));
130+
131+
$node->append_conf("pg_hba.conf", qq(
132+
local replication all trust
133+
host replication all 127.0.0.1/32 trust
134+
host replication all ::1/128 trust
135+
));
136+
}
137+
}
138+
139+
sub start
140+
{
141+
my ($self) = @_;
142+
my $nodes = $self->{nodes};
143+
144+
foreach my $node (@$nodes)
145+
{
146+
$node->start();
147+
}
148+
}
149+
150+
sub stop
151+
{
152+
my ($self) = @_;
153+
my $nodes = $self->{nodes};
154+
155+
foreach my $node (@$nodes)
156+
{
157+
$node->stop();
158+
}
159+
}
160+
161+
sub psql
162+
{
163+
my ($self, $index, @args) = @_;
164+
my $node = $self->{nodes}->[$index];
165+
return $node->psql(@args);
166+
}
167+
168+
169+
my $cluster = new RemoteCluster('ssh-config');
170+
171+
print $cluster->{'nnodes'} . "\n";
172+
173+
$cluster->run(1, 'ls -la');
174+
175+
1;
176+
177+
178+
179+

contrib/mmts/testeaux/Vagrantfile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
# vi: set ft=ruby :
33

44
Vagrant.configure(2) do |config|
5-
config.vm.box = "ubuntu/ubuntu-15.04-snappy-core-stable"
5+
config.vm.box = "ubuntu/trusty64"
66

7-
5.times do |i|
8-
config.vm.define "n#{i+1}l"
7+
nhosts = 3
8+
9+
nhosts.times do |i|
10+
config.vm.define "node#{i}" do |node|
11+
node.vm.hostname = "node#{i}"
12+
if i == nhosts - 1
13+
node.vm.provision :ansible do |ansible|
14+
ansible.limit = "all"
15+
ansible.playbook = "provision.yml"
16+
end
17+
end
18+
end
919
end
1020

1121
end

contrib/mmts/testeaux/provision.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
3+
- hosts: all
4+
5+
roles:
6+
- role: kelvich.postgres
7+
pg_port: 5432
8+
pg_repo: https://github.com/postgrespro/postgres_cluster.git
9+
pg_version_tag: master
10+
pg_destroy_and_init: true
11+
pg_config_role:
12+
- line: "multimaster.buffer_size = 65536"
13+
14+
tasks:
15+
- name: generate connstrings
16+
set_fact:
17+
connstr: "host={{item}} user={{ansible_ssh_user}} dbname=postgres"
18+
with_items:
19+
groups['all'] | reverse | batch(nnodes | d(3) | int) | first
20+
register: connstrs
21+
22+
- name: make a list
23+
set_fact:
24+
connections: "{{ connstrs.results | map(attribute='ansible_facts.connstr') | join(', ') }}"
25+
26+
- debug: var=hostvars
27+
28+
- debug: var=inventory_hostname
29+
30+
#- debug: var=hostvars[inventory_hostname]
31+
32+
- name: build raftable
33+
shell: "make clean && make -j {{makejobs}} install"
34+
args:
35+
chdir: "{{pg_src}}/contrib/raftable"
36+
37+
- name: build multimaster
38+
shell: "make clean && make -j {{makejobs}} install"
39+
args:
40+
chdir: "{{pg_src}}/contrib/mmts"
41+
42+
- name: enable dtm extension on datanodes
43+
lineinfile:
44+
dest: "{{pg_datadir}}/postgresql.conf"
45+
line: "{{item}}"
46+
state: present
47+
with_items:
48+
- "wal_level = logical"
49+
- "max_wal_senders = 10"
50+
- "wal_sender_timeout = 0"
51+
- "max_replication_slots = 10"
52+
- "max_connections = 200"
53+
- "max_worker_processes = 100"
54+
- "shared_preload_libraries = 'raftable,multimaster'"
55+
- "multimaster.conn_strings = '{{connections}}'"
56+
- "multimaster.node_id = {{ inventory_hostname | regex_replace('([0-9]+)', '\\1') }}"
57+
- "multimaster.buffer_size = 65536"
58+
- "multimaster.queue_size = 1073741824"
59+
- "multimaster.arbiter_port = 5600"
60+
- "multimaster.vacuum_delay = 1"
61+
- "multimaster.workers = 32"
62+
- "multimaster.use_dtm = 1"
63+
64+
- name: restart postgrespro
65+
command: "{{pg_dst}}/bin/pg_ctl restart -w -D {{pg_datadir}} -l {{pg_datadir}}/pg.log"
66+
environment:
67+
LD_LIBRARY_PATH: "{{pg_dst}}/lib/"
68+

contrib/mmts/tests/appender.pgb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\set key :appender_id * :clients + :client_id
2+
begin;
3+
update t set v = v + 1 where k = :key;
4+
commit;

0 commit comments

Comments
 (0)