Skip to content

Commit a716805

Browse files
committed
Add forked etcd 2.2.1 code to allow rollback to 2.2.1 version
1 parent b31cf72 commit a716805

28 files changed

+2605
-0
lines changed

third_party/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ filegroup(
1313
name = "all-srcs",
1414
srcs = [
1515
":package-srcs",
16+
"//third_party/forked/etcd221/pkg/fileutil:all-srcs",
17+
"//third_party/forked/etcd221/wal:all-srcs",
1618
"//third_party/forked/etcd237/pkg/fileutil:all-srcs",
1719
"//third_party/forked/etcd237/wal:all-srcs",
1820
"//third_party/forked/golang/expansion:all-srcs",

third_party/forked/etcd221/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Forked from etcd 2.2 release branch to support migration from 3.0 WAL to 2.2 WAL format
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
licenses(["notice"])
4+
5+
load(
6+
"@io_bazel_rules_go//go:def.bzl",
7+
"go_library",
8+
"go_test",
9+
)
10+
11+
go_test(
12+
name = "go_default_test",
13+
srcs = [
14+
"fileutil_test.go",
15+
"lock_test.go",
16+
"preallocate_test.go",
17+
"purge_test.go",
18+
],
19+
library = ":go_default_library",
20+
tags = ["automanaged"],
21+
)
22+
23+
go_library(
24+
name = "go_default_library",
25+
srcs = [
26+
"fileutil.go",
27+
"lock_unix.go",
28+
"preallocate.go",
29+
"purge.go",
30+
],
31+
tags = ["automanaged"],
32+
deps = ["//vendor:github.com/coreos/pkg/capnslog"],
33+
)
34+
35+
filegroup(
36+
name = "package-srcs",
37+
srcs = glob(["**"]),
38+
tags = ["automanaged"],
39+
visibility = ["//visibility:private"],
40+
)
41+
42+
filegroup(
43+
name = "all-srcs",
44+
srcs = [":package-srcs"],
45+
tags = ["automanaged"],
46+
)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2015 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package fileutil
16+
17+
import (
18+
"io/ioutil"
19+
"os"
20+
"path"
21+
"sort"
22+
23+
"github.com/coreos/pkg/capnslog"
24+
)
25+
26+
const (
27+
privateFileMode = 0600
28+
)
29+
30+
var (
31+
plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "fileutil")
32+
)
33+
34+
// IsDirWriteable checks if dir is writable by writing and removing a file
35+
// to dir. It returns nil if dir is writable.
36+
func IsDirWriteable(dir string) error {
37+
f := path.Join(dir, ".touch")
38+
if err := ioutil.WriteFile(f, []byte(""), privateFileMode); err != nil {
39+
return err
40+
}
41+
return os.Remove(f)
42+
}
43+
44+
// ReadDir returns the filenames in the given directory in sorted order.
45+
func ReadDir(dirpath string) ([]string, error) {
46+
dir, err := os.Open(dirpath)
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer dir.Close()
51+
names, err := dir.Readdirnames(-1)
52+
if err != nil {
53+
return nil, err
54+
}
55+
sort.Strings(names)
56+
return names, nil
57+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2015 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package fileutil
16+
17+
import (
18+
"io/ioutil"
19+
"os"
20+
"path/filepath"
21+
"reflect"
22+
"testing"
23+
)
24+
25+
func TestIsDirWriteable(t *testing.T) {
26+
tmpdir, err := ioutil.TempDir("", "")
27+
if err != nil {
28+
t.Fatalf("unexpected ioutil.TempDir error: %v", err)
29+
}
30+
defer os.RemoveAll(tmpdir)
31+
if err := IsDirWriteable(tmpdir); err != nil {
32+
t.Fatalf("unexpected IsDirWriteable error: %v", err)
33+
}
34+
if err := os.Chmod(tmpdir, 0444); err != nil {
35+
t.Fatalf("unexpected os.Chmod error: %v", err)
36+
}
37+
if err := IsDirWriteable(tmpdir); err == nil {
38+
t.Fatalf("expected IsDirWriteable to error")
39+
}
40+
}
41+
42+
func TestReadDir(t *testing.T) {
43+
tmpdir, err := ioutil.TempDir("", "")
44+
defer os.RemoveAll(tmpdir)
45+
if err != nil {
46+
t.Fatalf("unexpected ioutil.TempDir error: %v", err)
47+
}
48+
files := []string{"def", "abc", "xyz", "ghi"}
49+
for _, f := range files {
50+
var fh *os.File
51+
fh, err = os.Create(filepath.Join(tmpdir, f))
52+
if err != nil {
53+
t.Fatalf("error creating file: %v", err)
54+
}
55+
if err := fh.Close(); err != nil {
56+
t.Fatalf("error closing file: %v", err)
57+
}
58+
}
59+
fs, err := ReadDir(tmpdir)
60+
if err != nil {
61+
t.Fatalf("error calling ReadDir: %v", err)
62+
}
63+
wfs := []string{"abc", "def", "ghi", "xyz"}
64+
if !reflect.DeepEqual(fs, wfs) {
65+
t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
66+
}
67+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2015 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package fileutil
16+
17+
import (
18+
"errors"
19+
"os"
20+
"syscall"
21+
"time"
22+
)
23+
24+
var (
25+
ErrLocked = errors.New("file already locked")
26+
)
27+
28+
type Lock interface {
29+
Name() string
30+
TryLock() error
31+
Lock() error
32+
Unlock() error
33+
Destroy() error
34+
}
35+
36+
type lock struct {
37+
fname string
38+
file *os.File
39+
}
40+
41+
func (l *lock) Name() string {
42+
return l.fname
43+
}
44+
45+
// TryLock acquires exclusivity on the lock without blocking
46+
func (l *lock) TryLock() error {
47+
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
48+
if err != nil {
49+
return err
50+
}
51+
52+
f, err := os.Open(l.fname)
53+
if err != nil {
54+
return ErrLocked
55+
}
56+
57+
l.file = f
58+
return nil
59+
}
60+
61+
// Lock acquires exclusivity on the lock with blocking
62+
func (l *lock) Lock() error {
63+
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
64+
if err != nil {
65+
return err
66+
}
67+
68+
for {
69+
f, err := os.Open(l.fname)
70+
if err == nil {
71+
l.file = f
72+
return nil
73+
}
74+
time.Sleep(10 * time.Millisecond)
75+
}
76+
}
77+
78+
// Unlock unlocks the lock
79+
func (l *lock) Unlock() error {
80+
return l.file.Close()
81+
}
82+
83+
func (l *lock) Destroy() error {
84+
return nil
85+
}
86+
87+
func NewLock(file string) (Lock, error) {
88+
l := &lock{fname: file}
89+
return l, nil
90+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2015 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build solaris
16+
17+
package fileutil
18+
19+
import (
20+
"errors"
21+
"os"
22+
"syscall"
23+
)
24+
25+
var (
26+
ErrLocked = errors.New("file already locked")
27+
)
28+
29+
type Lock interface {
30+
Name() string
31+
TryLock() error
32+
Lock() error
33+
Unlock() error
34+
Destroy() error
35+
}
36+
37+
type lock struct {
38+
fd int
39+
file *os.File
40+
}
41+
42+
func (l *lock) Name() string {
43+
return l.file.Name()
44+
}
45+
46+
// TryLock acquires exclusivity on the lock without blocking
47+
func (l *lock) TryLock() error {
48+
var lock syscall.Flock_t
49+
lock.Start = 0
50+
lock.Len = 0
51+
lock.Pid = 0
52+
lock.Type = syscall.F_WRLCK
53+
lock.Whence = 0
54+
lock.Pid = 0
55+
err := syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
56+
if err != nil && err == syscall.EAGAIN {
57+
return ErrLocked
58+
}
59+
return err
60+
}
61+
62+
// Lock acquires exclusivity on the lock without blocking
63+
func (l *lock) Lock() error {
64+
var lock syscall.Flock_t
65+
lock.Start = 0
66+
lock.Len = 0
67+
lock.Type = syscall.F_WRLCK
68+
lock.Whence = 0
69+
lock.Pid = 0
70+
return syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
71+
}
72+
73+
// Unlock unlocks the lock
74+
func (l *lock) Unlock() error {
75+
var lock syscall.Flock_t
76+
lock.Start = 0
77+
lock.Len = 0
78+
lock.Type = syscall.F_UNLCK
79+
lock.Whence = 0
80+
err := syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
81+
if err != nil && err == syscall.EAGAIN {
82+
return ErrLocked
83+
}
84+
return err
85+
}
86+
87+
func (l *lock) Destroy() error {
88+
return l.file.Close()
89+
}
90+
91+
func NewLock(file string) (Lock, error) {
92+
f, err := os.OpenFile(file, os.O_WRONLY, 0600)
93+
if err != nil {
94+
return nil, err
95+
}
96+
l := &lock{int(f.Fd()), f}
97+
return l, nil
98+
}

0 commit comments

Comments
 (0)