Skip to content

Commit

Permalink
Add forked etcd 2.2.1 code to allow rollback to 2.2.1 version
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-t committed Feb 10, 2017
1 parent b31cf72 commit a716805
Show file tree
Hide file tree
Showing 28 changed files with 2,605 additions and 0 deletions.
2 changes: 2 additions & 0 deletions third_party/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//third_party/forked/etcd221/pkg/fileutil:all-srcs",
"//third_party/forked/etcd221/wal:all-srcs",
"//third_party/forked/etcd237/pkg/fileutil:all-srcs",
"//third_party/forked/etcd237/wal:all-srcs",
"//third_party/forked/golang/expansion:all-srcs",
Expand Down
1 change: 1 addition & 0 deletions third_party/forked/etcd221/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Forked from etcd 2.2 release branch to support migration from 3.0 WAL to 2.2 WAL format
46 changes: 46 additions & 0 deletions third_party/forked/etcd221/pkg/fileutil/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)

go_test(
name = "go_default_test",
srcs = [
"fileutil_test.go",
"lock_test.go",
"preallocate_test.go",
"purge_test.go",
],
library = ":go_default_library",
tags = ["automanaged"],
)

go_library(
name = "go_default_library",
srcs = [
"fileutil.go",
"lock_unix.go",
"preallocate.go",
"purge.go",
],
tags = ["automanaged"],
deps = ["//vendor:github.com/coreos/pkg/capnslog"],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
57 changes: 57 additions & 0 deletions third_party/forked/etcd221/pkg/fileutil/fileutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fileutil

import (
"io/ioutil"
"os"
"path"
"sort"

"github.com/coreos/pkg/capnslog"
)

const (
privateFileMode = 0600
)

var (
plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "fileutil")
)

// IsDirWriteable checks if dir is writable by writing and removing a file
// to dir. It returns nil if dir is writable.
func IsDirWriteable(dir string) error {
f := path.Join(dir, ".touch")
if err := ioutil.WriteFile(f, []byte(""), privateFileMode); err != nil {
return err
}
return os.Remove(f)
}

// ReadDir returns the filenames in the given directory in sorted order.
func ReadDir(dirpath string) ([]string, error) {
dir, err := os.Open(dirpath)
if err != nil {
return nil, err
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
67 changes: 67 additions & 0 deletions third_party/forked/etcd221/pkg/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fileutil

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)

func TestIsDirWriteable(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected ioutil.TempDir error: %v", err)
}
defer os.RemoveAll(tmpdir)
if err := IsDirWriteable(tmpdir); err != nil {
t.Fatalf("unexpected IsDirWriteable error: %v", err)
}
if err := os.Chmod(tmpdir, 0444); err != nil {
t.Fatalf("unexpected os.Chmod error: %v", err)
}
if err := IsDirWriteable(tmpdir); err == nil {
t.Fatalf("expected IsDirWriteable to error")
}
}

func TestReadDir(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
defer os.RemoveAll(tmpdir)
if err != nil {
t.Fatalf("unexpected ioutil.TempDir error: %v", err)
}
files := []string{"def", "abc", "xyz", "ghi"}
for _, f := range files {
var fh *os.File
fh, err = os.Create(filepath.Join(tmpdir, f))
if err != nil {
t.Fatalf("error creating file: %v", err)
}
if err := fh.Close(); err != nil {
t.Fatalf("error closing file: %v", err)
}
}
fs, err := ReadDir(tmpdir)
if err != nil {
t.Fatalf("error calling ReadDir: %v", err)
}
wfs := []string{"abc", "def", "ghi", "xyz"}
if !reflect.DeepEqual(fs, wfs) {
t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
}
}
90 changes: 90 additions & 0 deletions third_party/forked/etcd221/pkg/fileutil/lock_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fileutil

import (
"errors"
"os"
"syscall"
"time"
)

var (
ErrLocked = errors.New("file already locked")
)

type Lock interface {
Name() string
TryLock() error
Lock() error
Unlock() error
Destroy() error
}

type lock struct {
fname string
file *os.File
}

func (l *lock) Name() string {
return l.fname
}

// TryLock acquires exclusivity on the lock without blocking
func (l *lock) TryLock() error {
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
if err != nil {
return err
}

f, err := os.Open(l.fname)
if err != nil {
return ErrLocked
}

l.file = f
return nil
}

// Lock acquires exclusivity on the lock with blocking
func (l *lock) Lock() error {
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
if err != nil {
return err
}

for {
f, err := os.Open(l.fname)
if err == nil {
l.file = f
return nil
}
time.Sleep(10 * time.Millisecond)
}
}

// Unlock unlocks the lock
func (l *lock) Unlock() error {
return l.file.Close()
}

func (l *lock) Destroy() error {
return nil
}

func NewLock(file string) (Lock, error) {
l := &lock{fname: file}
return l, nil
}
98 changes: 98 additions & 0 deletions third_party/forked/etcd221/pkg/fileutil/lock_solaris.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build solaris

package fileutil

import (
"errors"
"os"
"syscall"
)

var (
ErrLocked = errors.New("file already locked")
)

type Lock interface {
Name() string
TryLock() error
Lock() error
Unlock() error
Destroy() error
}

type lock struct {
fd int
file *os.File
}

func (l *lock) Name() string {
return l.file.Name()
}

// TryLock acquires exclusivity on the lock without blocking
func (l *lock) TryLock() error {
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Pid = 0
lock.Type = syscall.F_WRLCK
lock.Whence = 0
lock.Pid = 0
err := syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
if err != nil && err == syscall.EAGAIN {
return ErrLocked
}
return err
}

// Lock acquires exclusivity on the lock without blocking
func (l *lock) Lock() error {
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Type = syscall.F_WRLCK
lock.Whence = 0
lock.Pid = 0
return syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
}

// Unlock unlocks the lock
func (l *lock) Unlock() error {
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Type = syscall.F_UNLCK
lock.Whence = 0
err := syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
if err != nil && err == syscall.EAGAIN {
return ErrLocked
}
return err
}

func (l *lock) Destroy() error {
return l.file.Close()
}

func NewLock(file string) (Lock, error) {
f, err := os.OpenFile(file, os.O_WRONLY, 0600)
if err != nil {
return nil, err
}
l := &lock{int(f.Fd()), f}
return l, nil
}
Loading

0 comments on commit a716805

Please sign in to comment.