mirror of https://github.com/ethereum/go-ethereum
node: switching prometheus flock location to tsdb (#19376)
* node: switching prometheus flock location to tsdb * rookie mistakepull/19397/head
parent
d5cae48bae
commit
a8dd1f93c6
@ -1,87 +0,0 @@ |
|||||||
The Prometheus systems and service monitoring server |
|
||||||
Copyright 2012-2015 The Prometheus Authors |
|
||||||
|
|
||||||
This product includes software developed at |
|
||||||
SoundCloud Ltd. (http://soundcloud.com/). |
|
||||||
|
|
||||||
|
|
||||||
The following components are included in this product: |
|
||||||
|
|
||||||
Bootstrap |
|
||||||
http://getbootstrap.com |
|
||||||
Copyright 2011-2014 Twitter, Inc. |
|
||||||
Licensed under the MIT License |
|
||||||
|
|
||||||
bootstrap3-typeahead.js |
|
||||||
https://github.com/bassjobsen/Bootstrap-3-Typeahead |
|
||||||
Original written by @mdo and @fat |
|
||||||
Copyright 2014 Bass Jobsen @bassjobsen |
|
||||||
Licensed under the Apache License, Version 2.0 |
|
||||||
|
|
||||||
fuzzy |
|
||||||
https://github.com/mattyork/fuzzy |
|
||||||
Original written by @mattyork |
|
||||||
Copyright 2012 Matt York |
|
||||||
Licensed under the MIT License |
|
||||||
|
|
||||||
bootstrap-datetimepicker.js |
|
||||||
https://github.com/Eonasdan/bootstrap-datetimepicker |
|
||||||
Copyright 2015 Jonathan Peterson (@Eonasdan) |
|
||||||
Licensed under the MIT License |
|
||||||
|
|
||||||
moment.js |
|
||||||
https://github.com/moment/moment/ |
|
||||||
Copyright JS Foundation and other contributors |
|
||||||
Licensed under the MIT License |
|
||||||
|
|
||||||
Rickshaw |
|
||||||
https://github.com/shutterstock/rickshaw |
|
||||||
Copyright 2011-2014 by Shutterstock Images, LLC |
|
||||||
See https://github.com/shutterstock/rickshaw/blob/master/LICENSE for license details |
|
||||||
|
|
||||||
mustache.js |
|
||||||
https://github.com/janl/mustache.js |
|
||||||
Copyright 2009 Chris Wanstrath (Ruby) |
|
||||||
Copyright 2010-2014 Jan Lehnardt (JavaScript) |
|
||||||
Copyright 2010-2015 The mustache.js community |
|
||||||
Licensed under the MIT License |
|
||||||
|
|
||||||
jQuery |
|
||||||
https://jquery.org |
|
||||||
Copyright jQuery Foundation and other contributors |
|
||||||
Licensed under the MIT License |
|
||||||
|
|
||||||
Go support for Protocol Buffers - Google's data interchange format |
|
||||||
http://github.com/golang/protobuf/ |
|
||||||
Copyright 2010 The Go Authors |
|
||||||
See source code for license details. |
|
||||||
|
|
||||||
Go support for leveled logs, analogous to |
|
||||||
https://code.google.com/p/google-glog/ |
|
||||||
Copyright 2013 Google Inc. |
|
||||||
Licensed under the Apache License, Version 2.0 |
|
||||||
|
|
||||||
Support for streaming Protocol Buffer messages for the Go language (golang). |
|
||||||
https://github.com/matttproud/golang_protobuf_extensions |
|
||||||
Copyright 2013 Matt T. Proud |
|
||||||
Licensed under the Apache License, Version 2.0 |
|
||||||
|
|
||||||
DNS library in Go |
|
||||||
http://miek.nl/posts/2014/Aug/16/go-dns-package/ |
|
||||||
Copyright 2009 The Go Authors, 2011 Miek Gieben |
|
||||||
See https://github.com/miekg/dns/blob/master/LICENSE for license details. |
|
||||||
|
|
||||||
LevelDB key/value database in Go |
|
||||||
https://github.com/syndtr/goleveldb |
|
||||||
Copyright 2012 Suryandaru Triandana |
|
||||||
See https://github.com/syndtr/goleveldb/blob/master/LICENSE for license details. |
|
||||||
|
|
||||||
gosnappy - a fork of code.google.com/p/snappy-go |
|
||||||
https://github.com/syndtr/gosnappy |
|
||||||
Copyright 2011 The Snappy-Go Authors |
|
||||||
See https://github.com/syndtr/gosnappy/blob/master/LICENSE for license details. |
|
||||||
|
|
||||||
go-zookeeper - Native ZooKeeper client for Go |
|
||||||
https://github.com/samuel/go-zookeeper |
|
||||||
Copyright (c) 2013, Samuel Stauffer <samuel@descolada.com> |
|
||||||
See https://github.com/samuel/go-zookeeper/blob/master/LICENSE for license details. |
|
@ -0,0 +1,22 @@ |
|||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 !windows
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import "os" |
||||||
|
|
||||||
|
// OpenDir opens a directory for syncing.
|
||||||
|
func OpenDir(path string) (*os.File, error) { return os.Open(path) } |
@ -0,0 +1,46 @@ |
|||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 windows
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"os" |
||||||
|
"syscall" |
||||||
|
) |
||||||
|
|
||||||
|
// OpenDir opens a directory in windows with write access for syncing.
|
||||||
|
func OpenDir(path string) (*os.File, error) { |
||||||
|
fd, err := openDir(path) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return os.NewFile(uintptr(fd), path), nil |
||||||
|
} |
||||||
|
|
||||||
|
func openDir(path string) (fd syscall.Handle, err error) { |
||||||
|
if len(path) == 0 { |
||||||
|
return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND |
||||||
|
} |
||||||
|
pathp, err := syscall.UTF16PtrFromString(path) |
||||||
|
if err != nil { |
||||||
|
return syscall.InvalidHandle, err |
||||||
|
} |
||||||
|
access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE) |
||||||
|
sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE) |
||||||
|
createmode := uint32(syscall.OPEN_EXISTING) |
||||||
|
fl := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS) |
||||||
|
return syscall.CreateFile(pathp, access, sharemode, nil, createmode, fl, 0) |
||||||
|
} |
@ -0,0 +1,149 @@ |
|||||||
|
// Copyright 2018 The Prometheus Authors
|
||||||
|
// 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 provides utility methods used when dealing with the filesystem in tsdb.
|
||||||
|
// It is largely copied from github.com/coreos/etcd/pkg/fileutil to avoid the
|
||||||
|
// dependency chain it brings with it.
|
||||||
|
// Please check github.com/coreos/etcd for licensing information.
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"io/ioutil" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
"sort" |
||||||
|
"strings" |
||||||
|
) |
||||||
|
|
||||||
|
// CopyDirs copies all directories, subdirectories and files recursively including the empty folders.
|
||||||
|
// Source and destination must be full paths.
|
||||||
|
func CopyDirs(src, dest string) error { |
||||||
|
if err := os.MkdirAll(dest, 0777); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
files, err := readDirs(src) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
for _, f := range files { |
||||||
|
dp := filepath.Join(dest, f) |
||||||
|
sp := filepath.Join(src, f) |
||||||
|
|
||||||
|
stat, err := os.Stat(sp) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
// Empty directories are also created.
|
||||||
|
if stat.IsDir() { |
||||||
|
if err := os.MkdirAll(dp, 0777); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
if err := copyFile(sp, dp); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func copyFile(src, dest string) error { |
||||||
|
data, err := ioutil.ReadFile(src) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
err = ioutil.WriteFile(dest, data, 0644) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// readDirs reads the source directory recursively and
|
||||||
|
// returns relative paths to all files and empty directories.
|
||||||
|
func readDirs(src string) ([]string, error) { |
||||||
|
var files []string |
||||||
|
|
||||||
|
err := filepath.Walk(src, func(path string, f os.FileInfo, err error) error { |
||||||
|
relativePath := strings.TrimPrefix(path, src) |
||||||
|
if len(relativePath) > 0 { |
||||||
|
files = append(files, relativePath) |
||||||
|
} |
||||||
|
return nil |
||||||
|
}) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return files, nil |
||||||
|
} |
||||||
|
|
||||||
|
// 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 |
||||||
|
} |
||||||
|
|
||||||
|
// Rename safely renames a file.
|
||||||
|
func Rename(from, to string) error { |
||||||
|
if err := os.Rename(from, to); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
// Directory was renamed; sync parent dir to persist rename.
|
||||||
|
pdir, err := OpenDir(filepath.Dir(to)) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if err = Fsync(pdir); err != nil { |
||||||
|
pdir.Close() |
||||||
|
return err |
||||||
|
} |
||||||
|
return pdir.Close() |
||||||
|
} |
||||||
|
|
||||||
|
// Replace moves a file or directory to a new location and deletes any previous data.
|
||||||
|
// It is not atomic.
|
||||||
|
func Replace(from, to string) error { |
||||||
|
if err := os.RemoveAll(to); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
if err := os.Rename(from, to); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
// Directory was renamed; sync parent dir to persist rename.
|
||||||
|
pdir, err := OpenDir(filepath.Dir(to)) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if err = Fsync(pdir); err != nil { |
||||||
|
pdir.Close() |
||||||
|
return err |
||||||
|
} |
||||||
|
return pdir.Close() |
||||||
|
} |
@ -0,0 +1,61 @@ |
|||||||
|
// Copyright 2018 The Prometheus Authors
|
||||||
|
// 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 ( |
||||||
|
"os" |
||||||
|
|
||||||
|
"github.com/pkg/errors" |
||||||
|
) |
||||||
|
|
||||||
|
type MmapFile struct { |
||||||
|
f *os.File |
||||||
|
b []byte |
||||||
|
} |
||||||
|
|
||||||
|
func OpenMmapFile(path string) (*MmapFile, error) { |
||||||
|
f, err := os.Open(path) |
||||||
|
if err != nil { |
||||||
|
return nil, errors.Wrap(err, "try lock file") |
||||||
|
} |
||||||
|
info, err := f.Stat() |
||||||
|
if err != nil { |
||||||
|
return nil, errors.Wrap(err, "stat") |
||||||
|
} |
||||||
|
|
||||||
|
b, err := mmap(f, int(info.Size())) |
||||||
|
if err != nil { |
||||||
|
return nil, errors.Wrap(err, "mmap") |
||||||
|
} |
||||||
|
|
||||||
|
return &MmapFile{f: f, b: b}, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (f *MmapFile) Close() error { |
||||||
|
err0 := munmap(f.b) |
||||||
|
err1 := f.f.Close() |
||||||
|
|
||||||
|
if err0 != nil { |
||||||
|
return err0 |
||||||
|
} |
||||||
|
return err1 |
||||||
|
} |
||||||
|
|
||||||
|
func (f *MmapFile) File() *os.File { |
||||||
|
return f.f |
||||||
|
} |
||||||
|
|
||||||
|
func (f *MmapFile) Bytes() []byte { |
||||||
|
return f.b |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
// Copyright 2018 The Prometheus Authors
|
||||||
|
// 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 windows
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
const maxMapSize = 0x7FFFFFFF // 2GB
|
@ -0,0 +1,18 @@ |
|||||||
|
// Copyright 2018 The Prometheus Authors
|
||||||
|
// 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 windows
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
const maxMapSize = 0xFFFFFFFFFFFF // 256TB
|
@ -0,0 +1,30 @@ |
|||||||
|
// Copyright 2017 The Prometheus Authors
|
||||||
|
// 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 !windows,!plan9
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"os" |
||||||
|
|
||||||
|
"golang.org/x/sys/unix" |
||||||
|
) |
||||||
|
|
||||||
|
func mmap(f *os.File, length int) ([]byte, error) { |
||||||
|
return unix.Mmap(int(f.Fd()), 0, length, unix.PROT_READ, unix.MAP_SHARED) |
||||||
|
} |
||||||
|
|
||||||
|
func munmap(b []byte) (err error) { |
||||||
|
return unix.Munmap(b) |
||||||
|
} |
@ -0,0 +1,46 @@ |
|||||||
|
// Copyright 2017 The Prometheus Authors
|
||||||
|
// 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 ( |
||||||
|
"os" |
||||||
|
"syscall" |
||||||
|
"unsafe" |
||||||
|
) |
||||||
|
|
||||||
|
func mmap(f *os.File, size int) ([]byte, error) { |
||||||
|
low, high := uint32(size), uint32(size>>32) |
||||||
|
h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, high, low, nil) |
||||||
|
if h == 0 { |
||||||
|
return nil, os.NewSyscallError("CreateFileMapping", errno) |
||||||
|
} |
||||||
|
|
||||||
|
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(size)) |
||||||
|
if addr == 0 { |
||||||
|
return nil, os.NewSyscallError("MapViewOfFile", errno) |
||||||
|
} |
||||||
|
|
||||||
|
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil { |
||||||
|
return nil, os.NewSyscallError("CloseHandle", err) |
||||||
|
} |
||||||
|
|
||||||
|
return (*[maxMapSize]byte)(unsafe.Pointer(addr))[:size], nil |
||||||
|
} |
||||||
|
|
||||||
|
func munmap(b []byte) error { |
||||||
|
if err := syscall.UnmapViewOfFile((uintptr)(unsafe.Pointer(&b[0]))); err != nil { |
||||||
|
return os.NewSyscallError("UnmapViewOfFile", err) |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,54 @@ |
|||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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" |
||||||
|
"os" |
||||||
|
) |
||||||
|
|
||||||
|
// Preallocate tries to allocate the space for given
|
||||||
|
// file. This operation is only supported on linux by a
|
||||||
|
// few filesystems (btrfs, ext4, etc.).
|
||||||
|
// If the operation is unsupported, no error will be returned.
|
||||||
|
// Otherwise, the error encountered will be returned.
|
||||||
|
func Preallocate(f *os.File, sizeInBytes int64, extendFile bool) error { |
||||||
|
if sizeInBytes == 0 { |
||||||
|
// fallocate will return EINVAL if length is 0; skip
|
||||||
|
return nil |
||||||
|
} |
||||||
|
if extendFile { |
||||||
|
return preallocExtend(f, sizeInBytes) |
||||||
|
} |
||||||
|
return preallocFixed(f, sizeInBytes) |
||||||
|
} |
||||||
|
|
||||||
|
func preallocExtendTrunc(f *os.File, sizeInBytes int64) error { |
||||||
|
curOff, err := f.Seek(0, io.SeekCurrent) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
size, err := f.Seek(sizeInBytes, io.SeekEnd) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
if _, err = f.Seek(curOff, io.SeekStart); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
if sizeInBytes > size { |
||||||
|
return nil |
||||||
|
} |
||||||
|
return f.Truncate(sizeInBytes) |
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 ( |
||||||
|
"os" |
||||||
|
"syscall" |
||||||
|
"unsafe" |
||||||
|
) |
||||||
|
|
||||||
|
func preallocExtend(f *os.File, sizeInBytes int64) error { |
||||||
|
if err := preallocFixed(f, sizeInBytes); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
return preallocExtendTrunc(f, sizeInBytes) |
||||||
|
} |
||||||
|
|
||||||
|
func preallocFixed(f *os.File, sizeInBytes int64) error { |
||||||
|
fstore := &syscall.Fstore_t{ |
||||||
|
Flags: syscall.F_ALLOCATEALL, |
||||||
|
Posmode: syscall.F_PEOFPOSMODE, |
||||||
|
Length: sizeInBytes} |
||||||
|
p := unsafe.Pointer(fstore) |
||||||
|
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_PREALLOCATE), uintptr(p)) |
||||||
|
if errno == 0 || errno == syscall.ENOTSUP { |
||||||
|
return nil |
||||||
|
} |
||||||
|
return errno |
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 ( |
||||||
|
"os" |
||||||
|
"syscall" |
||||||
|
) |
||||||
|
|
||||||
|
func preallocExtend(f *os.File, sizeInBytes int64) error { |
||||||
|
// use mode = 0 to change size
|
||||||
|
err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes) |
||||||
|
if err != nil { |
||||||
|
errno, ok := err.(syscall.Errno) |
||||||
|
// not supported; fallback
|
||||||
|
// fallocate EINTRs frequently in some environments; fallback
|
||||||
|
if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) { |
||||||
|
return preallocExtendTrunc(f, sizeInBytes) |
||||||
|
} |
||||||
|
} |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
func preallocFixed(f *os.File, sizeInBytes int64) error { |
||||||
|
// use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE
|
||||||
|
err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes) |
||||||
|
if err != nil { |
||||||
|
errno, ok := err.(syscall.Errno) |
||||||
|
// treat not supported as nil error
|
||||||
|
if ok && errno == syscall.ENOTSUP { |
||||||
|
return nil |
||||||
|
} |
||||||
|
} |
||||||
|
return err |
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
// Copyright 2015 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 !linux,!darwin
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import "os" |
||||||
|
|
||||||
|
func preallocExtend(f *os.File, sizeInBytes int64) error { |
||||||
|
return preallocExtendTrunc(f, sizeInBytes) |
||||||
|
} |
||||||
|
|
||||||
|
func preallocFixed(f *os.File, sizeInBytes int64) error { return nil } |
@ -0,0 +1,29 @@ |
|||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 !linux,!darwin
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import "os" |
||||||
|
|
||||||
|
// Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform.
|
||||||
|
func Fsync(f *os.File) error { |
||||||
|
return f.Sync() |
||||||
|
} |
||||||
|
|
||||||
|
// Fdatasync is a wrapper around file.Sync(). Special handling is needed on linux platform.
|
||||||
|
func Fdatasync(f *os.File) error { |
||||||
|
return f.Sync() |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 darwin
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"os" |
||||||
|
"syscall" |
||||||
|
) |
||||||
|
|
||||||
|
// Fsync on HFS/OSX flushes the data on to the physical drive but the drive
|
||||||
|
// may not write it to the persistent media for quite sometime and it may be
|
||||||
|
// written in out-of-order sequence. Using F_FULLFSYNC ensures that the
|
||||||
|
// physical drive's buffer will also get flushed to the media.
|
||||||
|
func Fsync(f *os.File) error { |
||||||
|
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_FULLFSYNC), uintptr(0)) |
||||||
|
if errno == 0 { |
||||||
|
return nil |
||||||
|
} |
||||||
|
return errno |
||||||
|
} |
||||||
|
|
||||||
|
// Fdatasync on darwin platform invokes fcntl(F_FULLFSYNC) for actual persistence
|
||||||
|
// on physical drive media.
|
||||||
|
func Fdatasync(f *os.File) error { |
||||||
|
return Fsync(f) |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
// Copyright 2016 The etcd Authors
|
||||||
|
//
|
||||||
|
// 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 linux
|
||||||
|
|
||||||
|
package fileutil |
||||||
|
|
||||||
|
import ( |
||||||
|
"os" |
||||||
|
"syscall" |
||||||
|
) |
||||||
|
|
||||||
|
// Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform.
|
||||||
|
func Fsync(f *os.File) error { |
||||||
|
return f.Sync() |
||||||
|
} |
||||||
|
|
||||||
|
// Fdatasync is similar to fsync(), but does not flush modified metadata
|
||||||
|
// unless that metadata is needed in order to allow a subsequent data retrieval
|
||||||
|
// to be correctly handled.
|
||||||
|
func Fdatasync(f *os.File) error { |
||||||
|
return syscall.Fdatasync(int(f.Fd())) |
||||||
|
} |
Loading…
Reference in new issue