Compare commits

...

56 Commits

Author SHA1 Message Date
Ben Johnson
2e3dda89ad Merge pull request #174 from benbjohnson/fix-snapshot-only-restore
Fix snapshot-only restore
2021-04-24 07:51:50 -06:00
Ben Johnson
331f6072bf Fix snapshot-only restore
This commit fixes a bug introduced by parallel restore (03831e2)
where snapshot-only restores were not being handled correctly and
Litestream would hang indefinitely. Now the restore will check
explicitly for snapshot-only restores and exit the restore process
early to avoid WAL handling completely.
2021-04-24 07:48:25 -06:00
Ben Johnson
6acfbcbc64 Add Docker shield to README 2021-04-23 16:04:44 -06:00
Ben Johnson
f21ebcda28 Merge pull request #171 from benbjohnson/remove-release-binary
Remove uncompressed release binary
2021-04-22 16:50:11 -06:00
Ben Johnson
e366b348cd Remove uncompressed release binary
This commit removes the uncompressed binary from the release page.
It was originally added to simplify Docker but it turns out that
having to chmod the binary will double its size.
2021-04-22 16:46:01 -06:00
Ben Johnson
064158b060 Merge pull request #170 from benbjohnson/remove-sync-lock-2
Remove SQLite write lock during WAL sync
2021-04-22 16:40:29 -06:00
Ben Johnson
1d1fd6e686 Remove SQLite write lock during WAL sync (again)
This commit reattempts a change to remove the write lock that was
previously tried in 998e831. This change will reduce the number of
locks on the database which should help reduce error messages that
applications see when they do not have busy_timeout set.

In addition to the lock removal, a passive checkpoint is issued
immediately before the read lock is obtained to prevent additional
checkpoints by the application itself. SQLite does not support
checkpoints from an active transaction so it cannot be done afterward.
2021-04-22 16:35:04 -06:00
Ben Johnson
73f8de23a6 Merge pull request #169 from benbjohnson/credentials
Allow use of LITESTREAM prefixed environment variables
2021-04-22 16:01:23 -06:00
Ben Johnson
7f4325e814 Allow use of LITESTREAM prefixed environment variables
This commit adds optional `LITESTREAM_ACCESS_KEY_ID` and
`LITESTREAM_SECRET_ACCESS_KEY` environment variables that can
be used instead of their `AWS` counterparts. The AWS-prefixed
variables have caused some confusion with users who were not
using AWS S3.
2021-04-22 15:58:17 -06:00
Ben Johnson
ad4b84410d Merge pull request #168 from benbjohnson/relicense
Relicense to Apache 2
2021-04-21 16:34:50 -06:00
Ben Johnson
0b7906aaac APLv2 2021-04-21 16:32:05 -06:00
Ben Johnson
6fc6d151b1 Merge pull request #167 from benbjohnson/parallel-restore
Download WAL files in parallel during restore
2021-04-21 16:27:09 -06:00
Ben Johnson
03831e2d06 Download WAL files in parallel during restore
This commit changes the restore to download multiple WAL files to
the local disk in parallel while another goroutine applies those
files in order. Downloading & applying the WAL files in serial
reduces the total throughput as WAL files are typically made up of
multiple small files.
2021-04-21 16:07:29 -06:00
Ben Johnson
257b625749 Merge pull request #166 from benbjohnson/fix-restore-to-index
Fix snapshot selection during restore-by-index
2021-04-21 12:15:19 -06:00
Ben Johnson
1c01af4e69 Fix snapshot selection during restore-by-index
This commit fixes a bug where restoring to a specific index will
incorrectly choose the latest snapshot instead of choosing the
latest snapshot that occurred before the given index.
2021-04-21 12:09:05 -06:00
Ben Johnson
bbd0f3b33c Merge pull request #165 from benbjohnson/restore-hex-index
Use hex encoding for restore's index flag
2021-04-19 12:07:17 -06:00
Ben Johnson
9439822763 Use hex-encoding for restore's index flag
This commit changes the `-index` flag on the `restore` command by
parsing it as a hex number instead of a decimal number. This is
done because the index is represented in hex form everywhere else
in the application.
2021-04-19 12:05:38 -06:00
Ben Johnson
63e51d2050 Merge pull request #164 from benbjohnson/improve-restore-logging
Improve restoration logging
2021-04-18 09:35:56 -06:00
Ben Johnson
84830bc4ad Improve restoration logging
This commit splits out logging for downloading a WAL file and applying
the WAL file to the database to get more accurate timing measurements.
2021-04-18 09:33:53 -06:00
Ben Johnson
ce0a5d2820 Merge pull request #163 from benbjohnson/remove-dry-run
Remove -dry-run flag in restore
2021-04-18 09:26:01 -06:00
Ben Johnson
3ad157d841 Remove -dry-run flag in restore
This flag is being removed because it's not actually that useful
in practice and it just makes the restoration code more complicated.
2021-04-18 09:21:50 -06:00
Ben Johnson
a20e35c5cc Merge pull request #162 from benbjohnson/log-micro
Use microsecond resolution for logging
2021-04-18 09:21:27 -06:00
Ben Johnson
029921299c Use microsecond resolution for logging
This commit changes `log` from second to microsecond resolution to
improve debugging when restoring WAL files.
2021-04-18 09:18:44 -06:00
Ben Johnson
f8d6969a4f Merge pull request #161 from benbjohnson/fix-flakey-wal-updated-at-test
Ensure minimum wait time for TestDB_UpdatedAt/WAL test
2021-04-17 09:41:59 -06:00
Ben Johnson
1e8bce029f Ensure minimum wait time for TestDB_UpdatedAt/WAL test
This commit fixes an issue where the test can be flakey if run on
a system with a higher time resolution. It now waits a minimum of
at least 100ms.
2021-04-17 09:40:07 -06:00
Ben Johnson
b29fb7e2ba Merge pull request #160 from benbjohnson/netgo
Add osusergo & netgo tags for static builds
2021-04-17 09:21:09 -06:00
Ben Johnson
dbb69786d3 Add osusergo & netgo tags for static builds 2021-04-17 09:18:03 -06:00
Ben Johnson
c70e9c0ba8 Merge pull request #159 from benbjohnson/strip
Reduce binary size
2021-04-17 09:14:05 -06:00
Ben Johnson
dd8fdd8c8c Reduce binary size 2021-04-17 09:12:38 -06:00
Ben Johnson
dfd1b1b92d Merge pull request #158 from benbjohnson/sqlite-flags
Omit load extensions for static builds
2021-04-17 09:08:36 -06:00
Ben Johnson
c4c30e394d Omit load extensions for static builds 2021-04-17 09:06:54 -06:00
Ben Johnson
28673aeb01 Merge pull request #157 from benbjohnson/expand-env
Configuration file environment variable expansion
2021-04-16 09:32:11 -06:00
Ben Johnson
04ae010378 Configuration file environment variable expansion
This commit adds simple variable expansion using either `$FOO`
or `${FOO}` when evaluating the config file. This can be disabled
by any command by using the `-no-expand-env` flag.
2021-04-16 09:28:01 -06:00
Ben Johnson
cefbcb0460 Merge pull request #156 from benbjohnson/docker
Docker
2021-04-16 08:37:30 -06:00
Ben Johnson
01407c3c25 Add Docker image 2021-04-16 08:35:22 -06:00
Ben Johnson
66fdb208c7 Merge pull request #154 from benbjohnson/fix-not-database-error-message
Remove reference to "wal" in first db init command
2021-04-15 11:59:28 -06:00
Ben Johnson
247896b8b7 Remove reference to "wal" in first db init command
This commit changes the error message of the first SQL command
executed during initialization. Typically, it wraps the error with
a message of "enable wal" since it is enabling the WAL mode but
that can be confusing if the DB connection or file is invalid.

Instead, the error is returned as-is and we can determine the
source of the error since it is the only unwrapped DB-related error.
2021-04-15 11:51:22 -06:00
Ben Johnson
1e6e741f55 Merge pull request #151 from benbjohnson/arm6
Add arm6 release builds
2021-04-12 09:47:54 -06:00
Ben Johnson
b31daabf52 Add arm6 release builds 2021-04-11 10:27:45 -06:00
Ben Johnson
1ccb4ef922 Merge pull request #149 from benbjohnson/arm-cgo
Enable CGO for cross compilation
2021-04-10 12:49:00 -06:00
Ben Johnson
54f0659c7b Enable CGO for cross compilation 2021-04-10 12:35:43 -06:00
Ben Johnson
5c0b8536f0 Merge pull request #148 from benbjohnson/arm
Support ARM release builds
2021-04-10 08:48:32 -06:00
Ben Johnson
462330ead6 Support ARM release builds 2021-04-10 08:39:10 -06:00
Ben Johnson
178cf836b1 Move pull request template 2021-04-07 16:24:49 -06:00
Ben Johnson
f45c0d8560 Create pull_request_template.md 2021-04-07 16:23:11 -06:00
Ben Johnson
bb146e2c09 Merge pull request #141 from benbjohnson/skip-verify
Add skip-verify flag for using self-signed certificates
2021-03-27 08:02:55 -06:00
Ben Johnson
f1d2df3e73 Add skip-verify flag for using self-signed certificates
This commit adds a `skip-verify` flag to the replica configuration
so that it can be used with self-signed certificates. This is useful
when running a local instance of MinIO with TLS for testing.
2021-03-27 08:00:09 -06:00
Ben Johnson
ef39987cc7 Merge pull request #133 from benbjohnson/sigterm
Catch sigterm & add shutdown logging
2021-03-21 09:38:38 -06:00
Ben Johnson
ee0c4c62d8 Catch sigterm & add shutdown logging
This commit changes the signal handler for `replicate` to catch
`syscall.SIGTERM` for non-Windows installations. It also adds some
logging to indicat when a shutdown has been initiated and when it
has finished.
2021-03-21 09:36:13 -06:00
Ben Johnson
e2de7e852c Merge pull request #132 from benbjohnson/sync-on-close
Sync on close
2021-03-21 08:45:26 -06:00
Ben Johnson
0529ce74b7 Sync on close
This commit changes the `replicate` command so that it performs a
final DB sync & replica sync before it exits to ensure it has
backed up all WAL frames at the time of exit.
2021-03-21 08:43:55 -06:00
Ben Johnson
421693130c Merge pull request #131 from benbjohnson/if-replica-exists
Add `-if-replica-exists` flag to restore
2021-03-21 08:09:57 -06:00
Ben Johnson
4a17c81b91 Add -if-replica-exists flag to restore
This commit adds a flag to change the exit code when restoring
from a replica where there is no existing backup. When set,
finding no backup will return a `0` exit code. The command will
still fail if other errors occur.
2021-03-21 08:08:11 -06:00
Ben Johnson
ba068ea3f8 Upload raw release binaries 2021-03-20 09:50:48 -06:00
Ben Johnson
085974fe1d Merge pull request #130 from benbjohnson/static
Add static release builds
2021-03-20 09:42:53 -06:00
Ben Johnson
18598a10e6 Add static release builds 2021-03-20 09:41:19 -06:00
25 changed files with 809 additions and 868 deletions

7
.github/pull_request_template.md vendored Normal file
View File

@@ -0,0 +1,7 @@
Litestream is not accepting code contributions at this time. You can find a summary of why on the project's GitHub README:
https://github.com/benbjohnson/litestream#open-source-not-open-contribution
Web site & Documentation changes, however, are welcome. You can find that repository here:
https://github.com/benbjohnson/litestream.io

View File

@@ -3,10 +3,30 @@ on:
types: types:
- created - created
name: release name: release (linux)
jobs: jobs:
linux: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy:
matrix:
include:
- arch: amd64
cc: gcc
- arch: arm64
cc: aarch64-linux-gnu-gcc
- arch: arm
arm: 6
cc: arm-linux-gnueabi-gcc
- arch: arm
arm: 7
cc: arm-linux-gnueabihf-gcc
env:
GOOS: linux
GOARCH: ${{ matrix.arch }}
GOARM: ${{ matrix.arm }}
CC: ${{ matrix.cc }}
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/setup-go@v2 - uses: actions/setup-go@v2
@@ -16,6 +36,11 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }}
- name: Install cross-compilers
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf gcc-arm-linux-gnueabi
- name: Install nfpm - name: Install nfpm
run: | run: |
wget https://github.com/goreleaser/nfpm/releases/download/v2.2.3/nfpm_2.2.3_Linux_x86_64.tar.gz wget https://github.com/goreleaser/nfpm/releases/download/v2.2.3/nfpm_2.2.3_Linux_x86_64.tar.gz
@@ -23,23 +48,24 @@ jobs:
- name: Build litestream - name: Build litestream
run: | run: |
rm -rf dist
mkdir -p dist mkdir -p dist
cp etc/litestream.yml etc/litestream.service dist cp etc/litestream.yml etc/litestream.service dist
cat etc/nfpm.yml | LITESTREAM_VERSION=${{ steps.release.outputs.tag_name }} envsubst > dist/nfpm.yml cat etc/nfpm.yml | LITESTREAM_VERSION=${{ steps.release.outputs.tag_name }} envsubst > dist/nfpm.yml
go build -ldflags "-X 'main.Version=${{ steps.release.outputs.tag_name }}'" -o dist/litestream ./cmd/litestream CGO_ENABLED=1 go build -ldflags "-s -w -X 'main.Version=${{ steps.release.outputs.tag_name }}'" -o dist/litestream ./cmd/litestream
cd dist cd dist
tar -czvf litestream-${{ steps.release.outputs.tag_name }}-linux-amd64.tar.gz litestream tar -czvf litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}.tar.gz litestream
../nfpm pkg --config nfpm.yml --packager deb --target litestream-${{ steps.release.outputs.tag_name }}-linux-amd64.deb ../nfpm pkg --config nfpm.yml --packager deb --target litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}.deb
- name: Upload release binary - name: Upload release tarball
uses: actions/upload-release-asset@v1.0.2 uses: actions/upload-release-asset@v1.0.2
env: env:
GITHUB_TOKEN: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }}
with: with:
upload_url: ${{ steps.release.outputs.upload_url }} upload_url: ${{ steps.release.outputs.upload_url }}
asset_path: ./dist/litestream-${{ steps.release.outputs.tag_name }}-linux-amd64.tar.gz asset_path: ./dist/litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}.tar.gz
asset_name: litestream-${{ steps.release.outputs.tag_name }}-linux-amd64.tar.gz asset_name: litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}.tar.gz
asset_content_type: application/gzip asset_content_type: application/gzip
- name: Upload debian package - name: Upload debian package
@@ -48,6 +74,6 @@ jobs:
GITHUB_TOKEN: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }}
with: with:
upload_url: ${{ steps.release.outputs.upload_url }} upload_url: ${{ steps.release.outputs.upload_url }}
asset_path: ./dist/litestream-${{ steps.release.outputs.tag_name }}-linux-amd64.deb asset_path: ./dist/litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}.deb
asset_name: litestream-${{ steps.release.outputs.tag_name }}-linux-amd64.deb asset_name: litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}.deb
asset_content_type: application/octet-stream asset_content_type: application/octet-stream

View File

@@ -0,0 +1,60 @@
on:
release:
types:
- created
name: release (linux/static)
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- arch: amd64
cc: gcc
- arch: arm64
cc: aarch64-linux-gnu-gcc
- arch: arm
arm: 6
cc: arm-linux-gnueabi-gcc
- arch: arm
arm: 7
cc: arm-linux-gnueabihf-gcc
env:
GOOS: linux
GOARCH: ${{ matrix.arch }}
GOARM: ${{ matrix.arm }}
CC: ${{ matrix.cc }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- id: release
uses: bruceadams/get-release@v1.2.2
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Install cross-compilers
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf gcc-arm-linux-gnueabi
- name: Build litestream
run: |
rm -rf dist
mkdir -p dist
CGO_ENABLED=1 go build -ldflags "-s -w -extldflags "-static" -X 'main.Version=${{ steps.release.outputs.tag_name }}'" -tags osusergo,netgo,sqlite_omit_load_extension -o dist/litestream ./cmd/litestream
cd dist
tar -czvf litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}-static.tar.gz litestream
- name: Upload release tarball
uses: actions/upload-release-asset@v1.0.2
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.release.outputs.upload_url }}
asset_path: ./dist/litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}-static.tar.gz
asset_name: litestream-${{ steps.release.outputs.tag_name }}-${{ env.GOOS }}-${{ env.GOARCH }}${{ env.GOARM }}-static.tar.gz
asset_content_type: application/gzip

11
Dockerfile Normal file
View File

@@ -0,0 +1,11 @@
FROM golang:1.16 as builder
WORKDIR /src/litestream
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
go build -ldflags '-s -w -extldflags "-static"' -tags osusergo,netgo,sqlite_omit_load_extension -o /usr/local/bin/litestream ./cmd/litestream
FROM alpine
COPY --from=builder /usr/local/bin/litestream /usr/local/bin/litestream
ENTRYPOINT ["/usr/local/bin/litestream"]
CMD []

858
LICENSE
View File

@@ -1,674 +1,202 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Apache License
Everyone is permitted to copy and distribute verbatim copies Version 2.0, January 2004
of this license document, but changing it is not allowed. http://www.apache.org/licenses/
Preamble TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
The GNU General Public License is a free, copyleft license for 1. Definitions.
software and other kinds of works.
"License" shall mean the terms and conditions for use, reproduction,
The licenses for most software and other practical works are designed and distribution as defined by Sections 1 through 9 of this document.
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to "Licensor" shall mean the copyright owner or entity authorized by
share and change all versions of a program--to make sure it remains free the copyright owner that is granting the License.
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to "Legal Entity" shall mean the union of the acting entity and all
any other work released this way by its authors. You can apply it to other entities that control, are controlled by, or are under common
your programs, too. control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
When we speak of free software, we are referring to freedom, not direction or management of such entity, whether by contract or
price. Our General Public Licenses are designed to make sure that you otherwise, or (ii) ownership of fifty percent (50%) or more of the
have the freedom to distribute copies of free software (and charge for outstanding shares, or (iii) beneficial ownership of such entity.
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new "You" (or "Your") shall mean an individual or Legal Entity
free programs, and that you know you can do these things. exercising permissions granted by this License.
To protect your rights, we need to prevent others from denying you "Source" form shall mean the preferred form for making modifications,
these rights or asking you to surrender the rights. Therefore, you have including but not limited to software source code, documentation
certain responsibilities if you distribute copies of the software, or if source, and configuration files.
you modify it: responsibilities to respect the freedom of others.
"Object" form shall mean any form resulting from mechanical
For example, if you distribute copies of such a program, whether transformation or translation of a Source form, including but
gratis or for a fee, you must pass on to the recipients the same not limited to compiled object code, generated documentation,
freedoms that you received. You must make sure that they, too, receive and conversions to other media types.
or can get the source code. And you must show them these terms so they
know their rights. "Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
Developers that use the GNU GPL protect your rights with two steps: copyright notice that is included in or attached to the work
(1) assert copyright on the software, and (2) offer you this License (an example is provided in the Appendix below).
giving you legal permission to copy, distribute and/or modify it.
"Derivative Works" shall mean any work, whether in Source or Object
For the developers' and authors' protection, the GPL clearly explains form, that is based on (or derived from) the Work and for which the
that there is no warranty for this free software. For both users' and editorial revisions, annotations, elaborations, or other modifications
authors' sake, the GPL requires that modified versions be marked as represent, as a whole, an original work of authorship. For the purposes
changed, so that their problems will not be attributed erroneously to of this License, Derivative Works shall not include works that remain
authors of previous versions. separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer "Contribution" shall mean any work of authorship, including
can do so. This is fundamentally incompatible with the aim of the original version of the Work and any modifications or additions
protecting users' freedom to change the software. The systematic to that Work or Derivative Works thereof, that is intentionally
pattern of such abuse occurs in the area of products for individuals to submitted to Licensor for inclusion in the Work by the copyright owner
use, which is precisely where it is most unacceptable. Therefore, we or by an individual or Legal Entity authorized to submit on behalf of
have designed this version of the GPL to prohibit the practice for those the copyright owner. For the purposes of this definition, "submitted"
products. If such problems arise substantially in other domains, we means any form of electronic, verbal, or written communication sent
stand ready to extend this provision to those domains in future versions to the Licensor or its representatives, including but not limited to
of the GPL, as needed to protect the freedom of users. communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Finally, every program is threatened constantly by software patents. Licensor for the purpose of discussing and improving the Work, but
States should not allow patents to restrict development and use of excluding communication that is conspicuously marked or otherwise
software on general-purpose computers, but in those that do, we wish to designated in writing by the copyright owner as "Not a Contribution."
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that "Contributor" shall mean Licensor and any individual or Legal Entity
patents cannot be used to render the program non-free. on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
The precise terms and conditions for copying, distribution and
modification follow. 2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
TERMS AND CONDITIONS worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
0. Definitions. publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
"This License" refers to version 3 of the GNU General Public License.
3. Grant of Patent License. Subject to the terms and conditions of
"Copyright" also means copyright-like laws that apply to other kinds of this License, each Contributor hereby grants to You a perpetual,
works, such as semiconductor masks. worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
"The Program" refers to any copyrightable work licensed under this use, offer to sell, sell, import, and otherwise transfer the Work,
License. Each licensee is addressed as "you". "Licensees" and where such license applies only to those patent claims licensable
"recipients" may be individuals or organizations. by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
To "modify" a work means to copy from or adapt all or part of the work with the Work to which such Contribution(s) was submitted. If You
in a fashion requiring copyright permission, other than the making of an institute patent litigation against any entity (including a
exact copy. The resulting work is called a "modified version" of the cross-claim or counterclaim in a lawsuit) alleging that the Work
earlier work or a work "based on" the earlier work. or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
A "covered work" means either the unmodified Program or a work based granted to You under this License for that Work shall terminate
on the Program. as of the date such litigation is filed.
To "propagate" a work means to do anything with it that, without 4. Redistribution. You may reproduce and distribute copies of the
permission, would make you directly or secondarily liable for Work or Derivative Works thereof in any medium, with or without
infringement under applicable copyright law, except executing it on a modifications, and in Source or Object form, provided that You
computer or modifying a private copy. Propagation includes copying, meet the following conditions:
distribution (with or without modification), making available to the
public, and in some countries other activities as well. (a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through (b) You must cause any modified files to carry prominent notices
a computer network, with no transfer of a copy, is not conveying. stating that You changed the files; and
An interactive user interface displays "Appropriate Legal Notices" (c) You must retain, in the Source form of any Derivative Works
to the extent that it includes a convenient and prominently visible that You distribute, all copyright, patent, trademark, and
feature that (1) displays an appropriate copyright notice, and (2) attribution notices from the Source form of the Work,
tells the user that there is no warranty for the work (except to the excluding those notices that do not pertain to any part of
extent that warranties are provided), that licensees may convey the the Derivative Works; and
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a (d) If the Work includes a "NOTICE" text file as part of its
menu, a prominent item in the list meets this criterion. distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
1. Source Code. within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
The "source code" for a work means the preferred form of the work of the following places: within a NOTICE text file distributed
for making modifications to it. "Object code" means any non-source as part of the Derivative Works; within the Source form or
form of a work. documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
A "Standard Interface" means an interface that either is an official wherever such third-party notices normally appear. The contents
standard defined by a recognized standards body, or, in the case of of the NOTICE file are for informational purposes only and
interfaces specified for a particular programming language, one that do not modify the License. You may add Your own attribution
is widely used among developers working in that language. notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
The "System Libraries" of an executable work include anything, other that such additional attribution notices cannot be construed
than the work as a whole, that (a) is included in the normal form of as modifying the License.
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that You may add Your own copyright statement to Your modifications and
Major Component, or to implement a Standard Interface for which an may provide additional or different license terms and conditions
implementation is available to the public in source code form. A for use, reproduction, or distribution of Your modifications, or
"Major Component", in this context, means a major essential component for any such Derivative Works as a whole, provided Your use,
(kernel, window system, and so on) of the specific operating system reproduction, and distribution of the Work otherwise complies with
(if any) on which the executable work runs, or a compiler used to the conditions stated in this License.
produce the work, or an object code interpreter used to run it.
5. Submission of Contributions. Unless You explicitly state otherwise,
The "Corresponding Source" for a work in object code form means all any Contribution intentionally submitted for inclusion in the Work
the source code needed to generate, install, and (for an executable by You to the Licensor shall be under the terms and conditions of
work) run the object code and to modify the work, including scripts to this License, without any additional terms or conditions.
control those activities. However, it does not include the work's Notwithstanding the above, nothing herein shall supersede or modify
System Libraries, or general-purpose tools or generally available free the terms of any separate license agreement you may have executed
programs which are used unmodified in performing those activities but with Licensor regarding such Contributions.
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for 6. Trademarks. This License does not grant permission to use the trade
the work, and the source code for shared libraries and dynamically names, trademarks, service marks, or product names of the Licensor,
linked subprograms that the work is specifically designed to require, except as required for reasonable and customary use in describing the
such as by intimate data communication or control flow between those origin of the Work and reproducing the content of the NOTICE file.
subprograms and other parts of the work.
7. Disclaimer of Warranty. Unless required by applicable law or
The Corresponding Source need not include anything that users agreed to in writing, Licensor provides the Work (and each
can regenerate automatically from other parts of the Corresponding Contributor provides its Contributions) on an "AS IS" BASIS,
Source. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
The Corresponding Source for a work in source code form is that of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
same work. PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
2. Basic Permissions. risks associated with Your exercise of permissions under this License.
All rights granted under this License are granted for the term of 8. Limitation of Liability. In no event and under no legal theory,
copyright on the Program, and are irrevocable provided the stated whether in tort (including negligence), contract, or otherwise,
conditions are met. This License explicitly affirms your unlimited unless required by applicable law (such as deliberate and grossly
permission to run the unmodified Program. The output from running a negligent acts) or agreed to in writing, shall any Contributor be
covered work is covered by this License only if the output, given its liable to You for damages, including any direct, indirect, special,
content, constitutes a covered work. This License acknowledges your incidental, or consequential damages of any character arising as a
rights of fair use or other equivalent, as provided by copyright law. result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
You may make, run and propagate covered works that you do not work stoppage, computer failure or malfunction, or any and all
convey, without conditions so long as your license otherwise remains other commercial damages or losses), even if such Contributor
in force. You may convey covered works to others for the sole purpose has been advised of the possibility of such damages.
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with 9. Accepting Warranty or Additional Liability. While redistributing
the terms of this License in conveying all material for which you do the Work or Derivative Works thereof, You may choose to offer,
not control copyright. Those thus making or running the covered works and charge a fee for, acceptance of support, warranty, indemnity,
for you must do so exclusively on your behalf, under your direction or other liability obligations and/or rights consistent with this
and control, on terms that prohibit them from making any copies of License. However, in accepting such obligations, You may act only
your copyrighted material outside their relationship with you. on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
Conveying under any other circumstances is permitted solely under defend, and hold each Contributor harmless for any liability
the conditions stated below. Sublicensing is not allowed; section 10 incurred by, or claims asserted against, such Contributor by reason
makes it unnecessary. of your accepting any such warranty or additional liability.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs APPENDIX: How to apply the Apache License to your work.
If you develop a new program, and you want it to be of the greatest To apply the Apache License to your work, attach the following
possible use to the public, the best way to achieve this is to make it boilerplate notice, with the fields enclosed by brackets "[]"
free software which everyone can redistribute and change under these terms. replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
To do so, attach the following notices to the program. It is safest Copyright [yyyy] [name of copyright owner]
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.> Licensed under the Apache License, Version 2.0 (the "License");
Copyright (C) <year> <name of author> you may not use this file except in compliance with the License.
You may obtain a copy of the License at
This program is free software: you can redistribute it and/or modify http://www.apache.org/licenses/LICENSE-2.0
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, Unless required by applicable law or agreed to in writing, software
but WITHOUT ANY WARRANTY; without even the implied warranty of distributed under the License is distributed on an "AS IS" BASIS,
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
GNU General Public License for more details. See the License for the specific language governing permissions and
limitations under the License.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

View File

@@ -1,17 +1,26 @@
default: default:
docker:
docker build -t litestream .
dist-linux: dist-linux:
mkdir -p dist mkdir -p dist
cp etc/litestream.yml dist/litestream.yml cp etc/litestream.yml dist/litestream.yml
docker run --rm -v "${PWD}":/usr/src/litestream -w /usr/src/litestream -e GOOS=linux -e GOARCH=amd64 golang:1.15 go build -v -o dist/litestream ./cmd/litestream docker run --rm -v "${PWD}":/usr/src/litestream -w /usr/src/litestream -e GOOS=linux -e GOARCH=amd64 golang:1.16 go build -v -ldflags "-s -w" -o dist/litestream ./cmd/litestream
tar -cz -f dist/litestream-linux-amd64.tar.gz -C dist litestream tar -cz -f dist/litestream-linux-amd64.tar.gz -C dist litestream
dist-linux-arm:
docker run --rm -v "${PWD}":/usr/src/litestream -w /usr/src/litestream -e CGO_ENABLED=1 -e CC=arm-linux-gnueabihf-gcc -e GOOS=linux -e GOARCH=arm golang-xc:1.16 go build -v -o dist/litestream-linux-arm ./cmd/litestream
dist-linux-arm64:
docker run --rm -v "${PWD}":/usr/src/litestream -w /usr/src/litestream -e CGO_ENABLED=1 -e CC=aarch64-linux-gnu-gcc -e GOOS=linux -e GOARCH=arm64 golang-xc:1.16 go build -v -o dist/litestream-linux-arm64 ./cmd/litestream
dist-macos: dist-macos:
ifndef LITESTREAM_VERSION ifndef LITESTREAM_VERSION
$(error LITESTREAM_VERSION is undefined) $(error LITESTREAM_VERSION is undefined)
endif endif
mkdir -p dist mkdir -p dist
go build -v -ldflags "-X 'main.Version=${LITESTREAM_VERSION}'" -o dist/litestream ./cmd/litestream go build -v -ldflags "-s -w -X 'main.Version=${LITESTREAM_VERSION}'" -o dist/litestream ./cmd/litestream
gon etc/gon.hcl gon etc/gon.hcl
mv dist/litestream.zip dist/litestream-${LITESTREAM_VERSION}-darwin-amd64.zip mv dist/litestream.zip dist/litestream-${LITESTREAM_VERSION}-darwin-amd64.zip
openssl dgst -sha256 dist/litestream-${LITESTREAM_VERSION}-darwin-amd64.zip openssl dgst -sha256 dist/litestream-${LITESTREAM_VERSION}-darwin-amd64.zip

View File

@@ -2,6 +2,7 @@ Litestream
![GitHub release (latest by date)](https://img.shields.io/github/v/release/benbjohnson/litestream) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/benbjohnson/litestream)
![Status](https://img.shields.io/badge/status-beta-blue) ![Status](https://img.shields.io/badge/status-beta-blue)
![GitHub](https://img.shields.io/github/license/benbjohnson/litestream) ![GitHub](https://img.shields.io/github/license/benbjohnson/litestream)
[![Docker Pulls](https://img.shields.io/docker/pulls/litestream/litestream.svg?maxAge=604800)](https://hub.docker.com/r/litestream/litestream/)
![test](https://github.com/benbjohnson/litestream/workflows/test/badge.svg) ![test](https://github.com/benbjohnson/litestream/workflows/test/badge.svg)
========== ==========

View File

@@ -15,7 +15,7 @@ type DatabasesCommand struct{}
// Run executes the command. // Run executes the command.
func (c *DatabasesCommand) Run(ctx context.Context, args []string) (err error) { func (c *DatabasesCommand) Run(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-databases", flag.ContinueOnError) fs := flag.NewFlagSet("litestream-databases", flag.ContinueOnError)
configPath := registerConfigFlag(fs) configPath, noExpandEnv := registerConfigFlag(fs)
fs.Usage = c.Usage fs.Usage = c.Usage
if err := fs.Parse(args); err != nil { if err := fs.Parse(args); err != nil {
return err return err
@@ -27,7 +27,7 @@ func (c *DatabasesCommand) Run(ctx context.Context, args []string) (err error) {
if *configPath == "" { if *configPath == "" {
*configPath = DefaultConfigPath() *configPath = DefaultConfigPath()
} }
config, err := ReadConfigFile(*configPath) config, err := ReadConfigFile(*configPath, !*noExpandEnv)
if err != nil { if err != nil {
return err return err
} }
@@ -72,6 +72,9 @@ Arguments:
Specifies the configuration file. Specifies the configuration file.
Defaults to %s Defaults to %s
-no-expand-env
Disables environment variable expansion in configuration file.
`[1:], `[1:],
DefaultConfigPath(), DefaultConfigPath(),
) )

View File

@@ -18,7 +18,7 @@ type GenerationsCommand struct{}
// Run executes the command. // Run executes the command.
func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error) { func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-generations", flag.ContinueOnError) fs := flag.NewFlagSet("litestream-generations", flag.ContinueOnError)
configPath := registerConfigFlag(fs) configPath, noExpandEnv := registerConfigFlag(fs)
replicaName := fs.String("replica", "", "replica name") replicaName := fs.String("replica", "", "replica name")
fs.Usage = c.Usage fs.Usage = c.Usage
if err := fs.Parse(args); err != nil { if err := fs.Parse(args); err != nil {
@@ -45,7 +45,7 @@ func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error)
} }
// Load configuration. // Load configuration.
config, err := ReadConfigFile(*configPath) config, err := ReadConfigFile(*configPath, !*noExpandEnv)
if err != nil { if err != nil {
return err return err
} }
@@ -131,6 +131,9 @@ Arguments:
Specifies the configuration file. Specifies the configuration file.
Defaults to %s Defaults to %s
-no-expand-env
Disables environment variable expansion in configuration file.
-replica NAME -replica NAME
Optional, filters by replica. Optional, filters by replica.

View File

@@ -14,6 +14,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
@@ -60,6 +61,9 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
return runWindowsService(ctx) return runWindowsService(ctx)
} }
// Copy "LITESTEAM" environment credentials.
applyLitestreamEnv()
// Extract command name. // Extract command name.
var cmd string var cmd string
if len(args) > 0 { if len(args) > 0 {
@@ -79,8 +83,7 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
// Setup signal handler. // Setup signal handler.
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
ch := make(chan os.Signal, 1) ch := signalChan()
signal.Notify(ch, os.Interrupt)
go func() { <-ch; cancel() }() go func() { <-ch; cancel() }()
if err := c.Run(ctx); err != nil { if err := c.Run(ctx); err != nil {
@@ -90,9 +93,14 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
// Wait for signal to stop program. // Wait for signal to stop program.
<-ctx.Done() <-ctx.Done()
signal.Reset() signal.Reset()
fmt.Println("signal received, litestream shutting down")
// Gracefully close. // Gracefully close.
return c.Close() if err := c.Close(); err != nil {
return err
}
fmt.Println("litestream shut down")
return nil
case "restore": case "restore":
return (&RestoreCommand{}).Run(ctx, args) return (&RestoreCommand{}).Run(ctx, args)
@@ -175,7 +183,8 @@ func (c *Config) DBConfig(path string) *DBConfig {
} }
// ReadConfigFile unmarshals config from filename. Expands path if needed. // ReadConfigFile unmarshals config from filename. Expands path if needed.
func ReadConfigFile(filename string) (_ Config, err error) { // If expandEnv is true then environment variables are expanded in the config.
func ReadConfigFile(filename string, expandEnv bool) (_ Config, err error) {
config := DefaultConfig() config := DefaultConfig()
// Expand filename, if necessary. // Expand filename, if necessary.
@@ -184,12 +193,20 @@ func ReadConfigFile(filename string) (_ Config, err error) {
return config, err return config, err
} }
// Read & deserialize configuration. // Read configuration.
if buf, err := ioutil.ReadFile(filename); os.IsNotExist(err) { buf, err := ioutil.ReadFile(filename)
if os.IsNotExist(err) {
return config, fmt.Errorf("config file not found: %s", filename) return config, fmt.Errorf("config file not found: %s", filename)
} else if err != nil { } else if err != nil {
return config, err return config, err
} else if err := yaml.Unmarshal(buf, &config); err != nil { }
// Expand environment variables, if enabled.
if expandEnv {
buf = []byte(os.ExpandEnv(string(buf)))
}
if err := yaml.Unmarshal(buf, &config); err != nil {
return config, err return config, err
} }
@@ -272,6 +289,7 @@ type ReplicaConfig struct {
Bucket string `yaml:"bucket"` Bucket string `yaml:"bucket"`
Endpoint string `yaml:"endpoint"` Endpoint string `yaml:"endpoint"`
ForcePathStyle *bool `yaml:"force-path-style"` ForcePathStyle *bool `yaml:"force-path-style"`
SkipVerify bool `yaml:"skip-verify"`
} }
// NewReplicaFromConfig instantiates a replica for a DB based on a config. // NewReplicaFromConfig instantiates a replica for a DB based on a config.
@@ -343,7 +361,7 @@ func newS3ReplicaFromConfig(c *ReplicaConfig, db *litestream.DB) (_ *s3.Replica,
} }
bucket, path := c.Bucket, c.Path bucket, path := c.Bucket, c.Path
region, endpoint := c.Region, c.Endpoint region, endpoint, skipVerify := c.Region, c.Endpoint, c.SkipVerify
// Use path style if an endpoint is explicitly set. This works because the // Use path style if an endpoint is explicitly set. This works because the
// only service to not use path style is AWS which does not use an endpoint. // only service to not use path style is AWS which does not use an endpoint.
@@ -392,6 +410,7 @@ func newS3ReplicaFromConfig(c *ReplicaConfig, db *litestream.DB) (_ *s3.Replica,
r.Region = region r.Region = region
r.Endpoint = endpoint r.Endpoint = endpoint
r.ForcePathStyle = forcePathStyle r.ForcePathStyle = forcePathStyle
r.SkipVerify = skipVerify
if v := c.Retention; v > 0 { if v := c.Retention; v > 0 {
r.Retention = v r.Retention = v
@@ -411,6 +430,22 @@ func newS3ReplicaFromConfig(c *ReplicaConfig, db *litestream.DB) (_ *s3.Replica,
return r, nil return r, nil
} }
// applyLitestreamEnv copies "LITESTREAM" prefixed environment variables to
// their AWS counterparts as the "AWS" prefix can be confusing when using a
// non-AWS S3-compatible service.
func applyLitestreamEnv() {
if v, ok := os.LookupEnv("LITESTREAM_ACCESS_KEY_ID"); ok {
if _, ok := os.LookupEnv("AWS_ACCESS_KEY_ID"); !ok {
os.Setenv("AWS_ACCESS_KEY_ID", v)
}
}
if v, ok := os.LookupEnv("LITESTREAM_SECRET_ACCESS_KEY"); ok {
if _, ok := os.LookupEnv("AWS_SECRET_ACCESS_KEY"); !ok {
os.Setenv("AWS_SECRET_ACCESS_KEY", v)
}
}
}
// ParseReplicaURL parses a replica URL. // ParseReplicaURL parses a replica URL.
func ParseReplicaURL(s string) (scheme, host, urlpath string, err error) { func ParseReplicaURL(s string) (scheme, host, urlpath string, err error) {
u, err := url.Parse(s) u, err := url.Parse(s)
@@ -455,8 +490,9 @@ func DefaultConfigPath() string {
return defaultConfigPath return defaultConfigPath
} }
func registerConfigFlag(fs *flag.FlagSet) *string { func registerConfigFlag(fs *flag.FlagSet) (configPath *string, noExpandEnv *bool) {
return fs.String("config", "", "config path") return fs.String("config", "", "config path"),
fs.Bool("no-expand-env", false, "do not expand env vars in config")
} }
// expand returns an absolute path for s. // expand returns an absolute path for s.
@@ -481,3 +517,24 @@ func expand(s string) (string, error) {
} }
return filepath.Join(u.HomeDir, strings.TrimPrefix(s, prefix)), nil return filepath.Join(u.HomeDir, strings.TrimPrefix(s, prefix)), nil
} }
// indexVar allows the flag package to parse index flags as 4-byte hexadecimal values.
type indexVar int
// Ensure type implements interface.
var _ flag.Value = (*indexVar)(nil)
// String returns an 8-character hexadecimal value.
func (v *indexVar) String() string {
return fmt.Sprintf("%08x", int(*v))
}
// Set parses s into an integer from a hexadecimal value.
func (v *indexVar) Set(s string) error {
i, err := strconv.ParseInt(s, 16, 32)
if err != nil {
return fmt.Errorf("invalid hexadecimal format")
}
*v = indexVar(i)
return nil
}

View File

@@ -4,6 +4,9 @@ package main
import ( import (
"context" "context"
"os"
"os/signal"
"syscall"
) )
const defaultConfigPath = "/etc/litestream.yml" const defaultConfigPath = "/etc/litestream.yml"
@@ -15,3 +18,9 @@ func isWindowsService() (bool, error) {
func runWindowsService(ctx context.Context) error { func runWindowsService(ctx context.Context) error {
panic("cannot run windows service as unix process") panic("cannot run windows service as unix process")
} }
func signalChan() <-chan os.Signal {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
return ch
}

View File

@@ -2,6 +2,7 @@ package main_test
import ( import (
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -26,7 +27,7 @@ dbs:
t.Fatal(err) t.Fatal(err)
} }
config, err := main.ReadConfigFile(filename) config, err := main.ReadConfigFile(filename, true)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if got, want := config.AccessKeyID, `XXX`; got != want { } else if got, want := config.AccessKeyID, `XXX`; got != want {
@@ -39,6 +40,56 @@ dbs:
t.Fatalf("Replica.SecretAccessKey=%v, want %v", got, want) t.Fatalf("Replica.SecretAccessKey=%v, want %v", got, want)
} }
}) })
// Ensure environment variables are expanded.
t.Run("ExpandEnv", func(t *testing.T) {
os.Setenv("LITESTREAM_TEST_0129380", "/path/to/db")
os.Setenv("LITESTREAM_TEST_1872363", "s3://foo/bar")
filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(`
dbs:
- path: $LITESTREAM_TEST_0129380
replicas:
- url: ${LITESTREAM_TEST_1872363}
- url: ${LITESTREAM_TEST_NO_SUCH_ENV}
`[1:]), 0666); err != nil {
t.Fatal(err)
}
config, err := main.ReadConfigFile(filename, true)
if err != nil {
t.Fatal(err)
} else if got, want := config.DBs[0].Path, `/path/to/db`; got != want {
t.Fatalf("DB.Path=%v, want %v", got, want)
} else if got, want := config.DBs[0].Replicas[0].URL, `s3://foo/bar`; got != want {
t.Fatalf("Replica[0].URL=%v, want %v", got, want)
} else if got, want := config.DBs[0].Replicas[1].URL, ``; got != want {
t.Fatalf("Replica[1].URL=%v, want %v", got, want)
}
})
// Ensure environment variables are not expanded.
t.Run("NoExpandEnv", func(t *testing.T) {
os.Setenv("LITESTREAM_TEST_9847533", "s3://foo/bar")
filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(`
dbs:
- path: /path/to/db
replicas:
- url: ${LITESTREAM_TEST_9847533}
`[1:]), 0666); err != nil {
t.Fatal(err)
}
config, err := main.ReadConfigFile(filename, false)
if err != nil {
t.Fatal(err)
} else if got, want := config.DBs[0].Replicas[0].URL, `${LITESTREAM_TEST_9847533}`; got != want {
t.Fatalf("Replica.URL=%v, want %v", got, want)
}
})
} }
func TestNewFileReplicaFromConfig(t *testing.T) { func TestNewFileReplicaFromConfig(t *testing.T) {

View File

@@ -7,6 +7,7 @@ import (
"io" "io"
"log" "log"
"os" "os"
"os/signal"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc"
@@ -103,3 +104,9 @@ func (w *eventlogWriter) Write(p []byte) (n int, err error) {
elog := (*eventlog.Log)(w) elog := (*eventlog.Log)(w)
return 0, elog.Info(1, string(p)) return 0, elog.Info(1, string(p))
} }
func signalChan() <-chan os.Signal {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
return ch
}

View File

@@ -32,7 +32,7 @@ func NewReplicateCommand() *ReplicateCommand {
func (c *ReplicateCommand) ParseFlags(ctx context.Context, args []string) (err error) { func (c *ReplicateCommand) ParseFlags(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-replicate", flag.ContinueOnError) fs := flag.NewFlagSet("litestream-replicate", flag.ContinueOnError)
tracePath := fs.String("trace", "", "trace path") tracePath := fs.String("trace", "", "trace path")
configPath := registerConfigFlag(fs) configPath, noExpandEnv := registerConfigFlag(fs)
fs.Usage = c.Usage fs.Usage = c.Usage
if err := fs.Parse(args); err != nil { if err := fs.Parse(args); err != nil {
return err return err
@@ -58,7 +58,7 @@ func (c *ReplicateCommand) ParseFlags(ctx context.Context, args []string) (err e
if *configPath == "" { if *configPath == "" {
*configPath = DefaultConfigPath() *configPath = DefaultConfigPath()
} }
if c.Config, err = ReadConfigFile(*configPath); err != nil { if c.Config, err = ReadConfigFile(*configPath, !*noExpandEnv); err != nil {
return err return err
} }
} }
@@ -70,7 +70,7 @@ func (c *ReplicateCommand) ParseFlags(ctx context.Context, args []string) (err e
return err return err
} }
defer f.Close() defer f.Close()
litestream.Tracef = log.New(f, "", log.LstdFlags|log.LUTC|log.Lshortfile).Printf litestream.Tracef = log.New(f, "", log.LstdFlags|log.Lmicroseconds|log.LUTC|log.Lshortfile).Printf
} }
return nil return nil
@@ -168,6 +168,9 @@ Arguments:
Specifies the configuration file. Specifies the configuration file.
Defaults to %s Defaults to %s
-no-expand-env
Disables environment variable expansion in configuration file.
-trace PATH -trace PATH
Write verbose trace logging to PATH. Write verbose trace logging to PATH.

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strconv"
"time" "time"
"github.com/benbjohnson/litestream" "github.com/benbjohnson/litestream"
@@ -21,12 +22,13 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
opt.Verbose = true opt.Verbose = true
fs := flag.NewFlagSet("litestream-restore", flag.ContinueOnError) fs := flag.NewFlagSet("litestream-restore", flag.ContinueOnError)
configPath := registerConfigFlag(fs) configPath, noExpandEnv := registerConfigFlag(fs)
fs.StringVar(&opt.OutputPath, "o", "", "output path") fs.StringVar(&opt.OutputPath, "o", "", "output path")
fs.StringVar(&opt.ReplicaName, "replica", "", "replica name") fs.StringVar(&opt.ReplicaName, "replica", "", "replica name")
fs.StringVar(&opt.Generation, "generation", "", "generation name") fs.StringVar(&opt.Generation, "generation", "", "generation name")
fs.IntVar(&opt.Index, "index", opt.Index, "wal index") fs.Var((*indexVar)(&opt.Index), "index", "wal index")
fs.BoolVar(&opt.DryRun, "dry-run", false, "dry run") fs.IntVar(&opt.Parallelism, "parallelism", opt.Parallelism, "parallelism")
ifReplicaExists := fs.Bool("if-replica-exists", false, "")
timestampStr := fs.String("timestamp", "", "timestamp") timestampStr := fs.String("timestamp", "", "timestamp")
verbose := fs.Bool("v", false, "verbose output") verbose := fs.Bool("v", false, "verbose output")
fs.Usage = c.Usage fs.Usage = c.Usage
@@ -45,14 +47,9 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
} }
} }
// Verbose output is automatically enabled if dry run is specified.
if opt.DryRun {
*verbose = true
}
// Instantiate logger if verbose output is enabled. // Instantiate logger if verbose output is enabled.
if *verbose { if *verbose {
opt.Logger = log.New(os.Stderr, "", log.LstdFlags) opt.Logger = log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds)
} }
// Determine replica & generation to restore from. // Determine replica & generation to restore from.
@@ -68,13 +65,18 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
if *configPath == "" { if *configPath == "" {
*configPath = DefaultConfigPath() *configPath = DefaultConfigPath()
} }
if r, err = c.loadFromConfig(ctx, fs.Arg(0), *configPath, &opt); err != nil { if r, err = c.loadFromConfig(ctx, fs.Arg(0), *configPath, !*noExpandEnv, &opt); err != nil {
return err return err
} }
} }
// Return an error if no matching targets found. // Return an error if no matching targets found.
// If optional flag set, return success. Useful for automated recovery.
if opt.Generation == "" { if opt.Generation == "" {
if *ifReplicaExists {
fmt.Println("no matching backups found")
return nil
}
return fmt.Errorf("no matching backups found") return fmt.Errorf("no matching backups found")
} }
@@ -92,9 +94,9 @@ func (c *RestoreCommand) loadFromURL(ctx context.Context, replicaURL string, opt
} }
// loadFromConfig returns a replica & updates the restore options from a DB reference. // loadFromConfig returns a replica & updates the restore options from a DB reference.
func (c *RestoreCommand) loadFromConfig(ctx context.Context, dbPath, configPath string, opt *litestream.RestoreOptions) (litestream.Replica, error) { func (c *RestoreCommand) loadFromConfig(ctx context.Context, dbPath, configPath string, expandEnv bool, opt *litestream.RestoreOptions) (litestream.Replica, error) {
// Load configuration. // Load configuration.
config, err := ReadConfigFile(configPath) config, err := ReadConfigFile(configPath, expandEnv)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -144,6 +146,9 @@ Arguments:
Specifies the configuration file. Specifies the configuration file.
Defaults to %s Defaults to %s
-no-expand-env
Disables environment variable expansion in configuration file.
-replica NAME -replica NAME
Restore from a specific replica. Restore from a specific replica.
Defaults to replica with latest data. Defaults to replica with latest data.
@@ -153,7 +158,7 @@ Arguments:
Defaults to generation with latest data. Defaults to generation with latest data.
-index NUM -index NUM
Restore up to a specific WAL index (inclusive). Restore up to a specific hex-encoded WAL index (inclusive).
Defaults to use the highest available index. Defaults to use the highest available index.
-timestamp TIMESTAMP -timestamp TIMESTAMP
@@ -164,9 +169,12 @@ Arguments:
Output path of the restored database. Output path of the restored database.
Defaults to original DB path. Defaults to original DB path.
-dry-run -if-replica-exists
Prints all log output as if it were running but does Returns exit code of 0 if no backups found.
not perform actual restore.
-parallelism NUM
Determines the number of WAL files downloaded in parallel.
Defaults to `+strconv.Itoa(litestream.DefaultRestoreParallelism)+`.
-v -v
Verbose output. Verbose output.

View File

@@ -17,7 +17,7 @@ type SnapshotsCommand struct{}
// Run executes the command. // Run executes the command.
func (c *SnapshotsCommand) Run(ctx context.Context, args []string) (err error) { func (c *SnapshotsCommand) Run(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-snapshots", flag.ContinueOnError) fs := flag.NewFlagSet("litestream-snapshots", flag.ContinueOnError)
configPath := registerConfigFlag(fs) configPath, noExpandEnv := registerConfigFlag(fs)
replicaName := fs.String("replica", "", "replica name") replicaName := fs.String("replica", "", "replica name")
fs.Usage = c.Usage fs.Usage = c.Usage
if err := fs.Parse(args); err != nil { if err := fs.Parse(args); err != nil {
@@ -43,7 +43,7 @@ func (c *SnapshotsCommand) Run(ctx context.Context, args []string) (err error) {
} }
// Load configuration. // Load configuration.
config, err := ReadConfigFile(*configPath) config, err := ReadConfigFile(*configPath, !*noExpandEnv)
if err != nil { if err != nil {
return err return err
} }
@@ -112,6 +112,9 @@ Arguments:
Specifies the configuration file. Specifies the configuration file.
Defaults to %s Defaults to %s
-no-expand-env
Disables environment variable expansion in configuration file.
-replica NAME -replica NAME
Optional, filter by a specific replica. Optional, filter by a specific replica.

View File

@@ -17,7 +17,7 @@ type WALCommand struct{}
// Run executes the command. // Run executes the command.
func (c *WALCommand) Run(ctx context.Context, args []string) (err error) { func (c *WALCommand) Run(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-wal", flag.ContinueOnError) fs := flag.NewFlagSet("litestream-wal", flag.ContinueOnError)
configPath := registerConfigFlag(fs) configPath, noExpandEnv := registerConfigFlag(fs)
replicaName := fs.String("replica", "", "replica name") replicaName := fs.String("replica", "", "replica name")
generation := fs.String("generation", "", "generation name") generation := fs.String("generation", "", "generation name")
fs.Usage = c.Usage fs.Usage = c.Usage
@@ -44,7 +44,7 @@ func (c *WALCommand) Run(ctx context.Context, args []string) (err error) {
} }
// Load configuration. // Load configuration.
config, err := ReadConfigFile(*configPath) config, err := ReadConfigFile(*configPath, !*noExpandEnv)
if err != nil { if err != nil {
return err return err
} }
@@ -118,6 +118,9 @@ Arguments:
Specifies the configuration file. Specifies the configuration file.
Defaults to %s Defaults to %s
-no-expand-env
Disables environment variable expansion in configuration file.
-replica NAME -replica NAME
Optional, filter by a specific replica. Optional, filter by a specific replica.

269
db.go
View File

@@ -23,6 +23,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"golang.org/x/sync/errgroup"
) )
// Default DB settings. // Default DB settings.
@@ -291,18 +292,50 @@ func (db *DB) Open() (err error) {
// Close releases the read lock & closes the database. This method should only // Close releases the read lock & closes the database. This method should only
// be called by tests as it causes the underlying database to be checkpointed. // be called by tests as it causes the underlying database to be checkpointed.
func (db *DB) Close() (err error) { func (db *DB) Close() (err error) {
// Ensure replicas all stop replicating. return db.close(false)
for _, r := range db.Replicas {
r.Stop(true)
} }
// SoftClose closes everything but the underlying db connection. This method
// is available because the binary needs to avoid closing the database on exit
// to prevent autocheckpointing.
func (db *DB) SoftClose() (err error) {
return db.close(true)
}
func (db *DB) close(soft bool) (err error) {
db.cancel()
db.wg.Wait()
// Start a new context for shutdown since we canceled the DB context.
ctx := context.Background()
// Perform a final db sync, if initialized.
if db.db != nil {
if e := db.Sync(ctx); e != nil && err == nil {
err = e
}
}
// Ensure replicas perform a final sync and stop replicating.
for _, r := range db.Replicas {
if db.db != nil {
if e := r.Sync(ctx); e != nil && err == nil {
err = e
}
}
r.Stop(!soft)
}
// Release the read lock to allow other applications to handle checkpointing.
if db.rtx != nil { if db.rtx != nil {
if e := db.releaseReadLock(); e != nil && err == nil { if e := db.releaseReadLock(); e != nil && err == nil {
err = e err = e
} }
} }
if db.db != nil { // Only perform full close if this is not a soft close.
// This closes the underlying database connection which can clean up the WAL.
if !soft && db.db != nil {
if e := db.db.Close(); e != nil && err == nil { if e := db.db.Close(); e != nil && err == nil {
err = e err = e
} }
@@ -420,7 +453,7 @@ func (db *DB) init() (err error) {
// https://www.sqlite.org/pragma.html#pragma_journal_mode // https://www.sqlite.org/pragma.html#pragma_journal_mode
var mode string var mode string
if err := db.db.QueryRow(`PRAGMA journal_mode = wal;`).Scan(&mode); err != nil { if err := db.db.QueryRow(`PRAGMA journal_mode = wal;`).Scan(&mode); err != nil {
return fmt.Errorf("enable wal: %w", err) return err
} else if mode != "wal" { } else if mode != "wal" {
return fmt.Errorf("enable wal failed, mode=%q", mode) return fmt.Errorf("enable wal failed, mode=%q", mode)
} }
@@ -597,26 +630,6 @@ func (db *DB) cleanWAL() error {
return nil return nil
} }
// SoftClose closes everything but the underlying db connection. This method
// is available because the binary needs to avoid closing the database on exit
// to prevent autocheckpointing.
func (db *DB) SoftClose() (err error) {
db.cancel()
db.wg.Wait()
// Ensure replicas all stop replicating.
for _, r := range db.Replicas {
r.Stop(false)
}
if db.rtx != nil {
if e := db.releaseReadLock(); e != nil && err == nil {
err = e
}
}
return err
}
// acquireReadLock begins a read transaction on the database to prevent checkpointing. // acquireReadLock begins a read transaction on the database to prevent checkpointing.
func (db *DB) acquireReadLock() error { func (db *DB) acquireReadLock() error {
if db.rtx != nil { if db.rtx != nil {
@@ -711,7 +724,7 @@ func (db *DB) createGeneration() (string, error) {
} }
// Sync copies pending data from the WAL to the shadow WAL. // Sync copies pending data from the WAL to the shadow WAL.
func (db *DB) Sync() (err error) { func (db *DB) Sync(ctx context.Context) (err error) {
db.mu.Lock() db.mu.Lock()
defer db.mu.Unlock() defer db.mu.Unlock()
@@ -738,27 +751,6 @@ func (db *DB) Sync() (err error) {
return fmt.Errorf("ensure wal exists: %w", err) return fmt.Errorf("ensure wal exists: %w", err)
} }
// Start a transaction. This will be promoted immediately after.
tx, err := db.db.Begin()
if err != nil {
return fmt.Errorf("begin: %w", err)
}
// Ensure write transaction rolls back before returning.
defer func() {
if e := rollback(tx); e != nil && err == nil {
err = e
}
}()
// Insert into the lock table to promote to a write tx. The lock table
// insert will never actually occur because our tx will be rolled back,
// however, it will ensure our tx grabs the write lock. Unfortunately,
// we can't call "BEGIN IMMEDIATE" as we are already in a transaction.
if _, err := tx.ExecContext(db.ctx, `INSERT INTO _litestream_lock (id) VALUES (1);`); err != nil {
return fmt.Errorf("_litestream_lock: %w", err)
}
// Verify our last sync matches the current state of the WAL. // Verify our last sync matches the current state of the WAL.
// This ensures that we have an existing generation & that the last sync // This ensures that we have an existing generation & that the last sync
// position of the real WAL hasn't been overwritten by another process. // position of the real WAL hasn't been overwritten by another process.
@@ -805,16 +797,11 @@ func (db *DB) Sync() (err error) {
checkpoint = true checkpoint = true
} }
// Release write lock before checkpointing & exiting.
if err := tx.Rollback(); err != nil {
return fmt.Errorf("rollback write tx: %w", err)
}
// Issue the checkpoint. // Issue the checkpoint.
if checkpoint { if checkpoint {
changed = true changed = true
if err := db.checkpointAndInit(info.generation, checkpointMode); err != nil { if err := db.checkpointAndInit(ctx, info.generation, checkpointMode); err != nil {
return fmt.Errorf("checkpoint: mode=%v err=%w", checkpointMode, err) return fmt.Errorf("checkpoint: mode=%v err=%w", checkpointMode, err)
} }
} }
@@ -1325,7 +1312,7 @@ func (db *DB) checkpoint(mode string) (err error) {
// checkpointAndInit performs a checkpoint on the WAL file and initializes a // checkpointAndInit performs a checkpoint on the WAL file and initializes a
// new shadow WAL file. // new shadow WAL file.
func (db *DB) checkpointAndInit(generation, mode string) error { func (db *DB) checkpointAndInit(ctx context.Context, generation, mode string) error {
shadowWALPath, err := db.CurrentShadowWALPath(generation) shadowWALPath, err := db.CurrentShadowWALPath(generation)
if err != nil { if err != nil {
return err return err
@@ -1368,7 +1355,7 @@ func (db *DB) checkpointAndInit(generation, mode string) error {
// insert will never actually occur because our tx will be rolled back, // insert will never actually occur because our tx will be rolled back,
// however, it will ensure our tx grabs the write lock. Unfortunately, // however, it will ensure our tx grabs the write lock. Unfortunately,
// we can't call "BEGIN IMMEDIATE" as we are already in a transaction. // we can't call "BEGIN IMMEDIATE" as we are already in a transaction.
if _, err := tx.ExecContext(db.ctx, `INSERT INTO _litestream_lock (id) VALUES (1);`); err != nil { if _, err := tx.ExecContext(ctx, `INSERT INTO _litestream_lock (id) VALUES (1);`); err != nil {
return fmt.Errorf("_litestream_lock: %w", err) return fmt.Errorf("_litestream_lock: %w", err)
} }
@@ -1410,7 +1397,7 @@ func (db *DB) monitor() {
} }
// Sync the database to the shadow WAL. // Sync the database to the shadow WAL.
if err := db.Sync(); err != nil && !errors.Is(err, context.Canceled) { if err := db.Sync(db.ctx); err != nil && !errors.Is(err, context.Canceled) {
log.Printf("%s: sync error: %s", db.path, err) log.Printf("%s: sync error: %s", db.path, err)
} }
} }
@@ -1422,13 +1409,13 @@ func (db *DB) monitor() {
// replica or generation or it will automatically choose the best one. Finally, // replica or generation or it will automatically choose the best one. Finally,
// a timestamp can be specified to restore the database to a specific // a timestamp can be specified to restore the database to a specific
// point-in-time. // point-in-time.
func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error { func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) (err error) {
// Validate options. // Validate options.
if opt.OutputPath == "" { if opt.OutputPath == "" {
return fmt.Errorf("output path required") return fmt.Errorf("output path required")
} else if opt.Generation == "" && opt.Index != math.MaxInt64 { } else if opt.Generation == "" && opt.Index != math.MaxInt32 {
return fmt.Errorf("must specify generation when restoring to index") return fmt.Errorf("must specify generation when restoring to index")
} else if opt.Index != math.MaxInt64 && !opt.Timestamp.IsZero() { } else if opt.Index != math.MaxInt32 && !opt.Timestamp.IsZero() {
return fmt.Errorf("cannot specify index & timestamp to restore") return fmt.Errorf("cannot specify index & timestamp to restore")
} }
@@ -1443,19 +1430,23 @@ func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error {
logPrefix = fmt.Sprintf("%s(%s)", db.Path(), r.Name()) logPrefix = fmt.Sprintf("%s(%s)", db.Path(), r.Name())
} }
// Ensure output path does not already exist (unless this is a dry run). // Ensure output path does not already exist.
if !opt.DryRun {
if _, err := os.Stat(opt.OutputPath); err == nil { if _, err := os.Stat(opt.OutputPath); err == nil {
return fmt.Errorf("cannot restore, output path already exists: %s", opt.OutputPath) return fmt.Errorf("cannot restore, output path already exists: %s", opt.OutputPath)
} else if err != nil && !os.IsNotExist(err) { } else if err != nil && !os.IsNotExist(err) {
return err return err
} }
}
// Find lastest snapshot that occurs before timestamp. // Find lastest snapshot that occurs before timestamp or index.
minWALIndex, err := SnapshotIndexAt(ctx, r, opt.Generation, opt.Timestamp) var minWALIndex int
if err != nil { if opt.Index < math.MaxInt32 {
return fmt.Errorf("cannot find snapshot index for restore: %w", err) if minWALIndex, err = SnapshotIndexByIndex(ctx, r, opt.Generation, opt.Index); err != nil {
return fmt.Errorf("cannot find snapshot index: %w", err)
}
} else {
if minWALIndex, err = SnapshotIndexAt(ctx, r, opt.Generation, opt.Timestamp); err != nil {
return fmt.Errorf("cannot find snapshot index by timestamp: %w", err)
}
} }
// Find the maximum WAL index that occurs before timestamp. // Find the maximum WAL index that occurs before timestamp.
@@ -1463,7 +1454,7 @@ func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error {
if err != nil { if err != nil {
return fmt.Errorf("cannot find max wal index for restore: %w", err) return fmt.Errorf("cannot find max wal index for restore: %w", err)
} }
logger.Printf("%s: starting restore: generation %s, index %08x-%08x", logPrefix, opt.Generation, minWALIndex, maxWALIndex) snapshotOnly := maxWALIndex == -1
// Initialize starting position. // Initialize starting position.
pos := Pos{Generation: opt.Generation, Index: minWALIndex} pos := Pos{Generation: opt.Generation, Index: minWALIndex}
@@ -1471,39 +1462,124 @@ func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error {
// Copy snapshot to output path. // Copy snapshot to output path.
logger.Printf("%s: restoring snapshot %s/%08x to %s", logPrefix, opt.Generation, minWALIndex, tmpPath) logger.Printf("%s: restoring snapshot %s/%08x to %s", logPrefix, opt.Generation, minWALIndex, tmpPath)
if !opt.DryRun {
if err := restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil { if err := restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil {
return fmt.Errorf("cannot restore snapshot: %w", err) return fmt.Errorf("cannot restore snapshot: %w", err)
} }
// If no WAL files available, move snapshot to final path & exit early.
if snapshotOnly {
logger.Printf("%s: snapshot only, finalizing database", logPrefix)
return os.Rename(tmpPath, opt.OutputPath)
} }
// Restore each WAL file until we reach our maximum index. // Begin processing WAL files.
logger.Printf("%s: restoring wal files: generation=%s index=[%08x,%08x]", logPrefix, opt.Generation, minWALIndex, maxWALIndex)
// Fill input channel with all WAL indexes to be loaded in order.
ch := make(chan int, maxWALIndex-minWALIndex+1)
for index := minWALIndex; index <= maxWALIndex; index++ { for index := minWALIndex; index <= maxWALIndex; index++ {
if !opt.DryRun { ch <- index
if err = restoreWAL(ctx, r, opt.Generation, index, tmpPath); os.IsNotExist(err) && index == minWALIndex && index == maxWALIndex {
logger.Printf("%s: no wal available, snapshot only", logPrefix)
break // snapshot file only, ignore error
} else if err != nil {
return fmt.Errorf("cannot restore wal: %w", err)
} }
close(ch)
// Track load state for each WAL.
var mu sync.Mutex
cond := sync.NewCond(&mu)
walStates := make([]walRestoreState, maxWALIndex-minWALIndex+1)
parallelism := opt.Parallelism
if parallelism < 1 {
parallelism = 1
} }
if opt.Verbose { // Download WAL files to disk in parallel.
logger.Printf("%s: restored wal %s/%08x", logPrefix, opt.Generation, index) g, ctx := errgroup.WithContext(ctx)
for i := 0; i < parallelism; i++ {
g.Go(func() error {
for {
select {
case <-ctx.Done():
cond.Broadcast()
return err
case index, ok := <-ch:
if !ok {
cond.Broadcast()
return nil
} }
startTime := time.Now()
err := downloadWAL(ctx, r, opt.Generation, index, tmpPath)
if err != nil {
err = fmt.Errorf("cannot download wal %s/%08x: %w", opt.Generation, index, err)
}
// Mark index as ready-to-apply and notify applying code.
mu.Lock()
walStates[index-minWALIndex] = walRestoreState{ready: true, err: err}
mu.Unlock()
cond.Broadcast()
// Returning the error here will cancel the other goroutines.
if err != nil {
return err
}
logger.Printf("%s: downloaded wal %s/%08x elapsed=%s",
logPrefix, opt.Generation, index,
time.Since(startTime).String(),
)
}
}
})
}
// Apply WAL files in order as they are ready.
for index := minWALIndex; index <= maxWALIndex; index++ {
// Wait until next WAL file is ready to apply.
mu.Lock()
for !walStates[index-minWALIndex].ready {
if err := ctx.Err(); err != nil {
return err
}
cond.Wait()
}
if err := walStates[index-minWALIndex].err; err != nil {
return err
}
mu.Unlock()
// Apply WAL to database file.
startTime := time.Now()
if err = applyWAL(ctx, index, tmpPath); err != nil {
return fmt.Errorf("cannot apply wal: %w", err)
}
logger.Printf("%s: applied wal %s/%08x elapsed=%s",
logPrefix, opt.Generation, index,
time.Since(startTime).String(),
)
}
// Ensure all goroutines finish. All errors should have been handled during
// the processing of WAL files but this ensures that all processing is done.
if err := g.Wait(); err != nil {
return err
} }
// Copy file to final location. // Copy file to final location.
logger.Printf("%s: renaming database from temporary location", logPrefix) logger.Printf("%s: renaming database from temporary location", logPrefix)
if !opt.DryRun {
if err := os.Rename(tmpPath, opt.OutputPath); err != nil { if err := os.Rename(tmpPath, opt.OutputPath); err != nil {
return err return err
} }
}
return nil return nil
} }
type walRestoreState struct {
ready bool
err error
}
// CalcRestoreTarget returns a replica & generation to restore from based on opt criteria. // CalcRestoreTarget returns a replica & generation to restore from based on opt criteria.
func (db *DB) CalcRestoreTarget(ctx context.Context, opt RestoreOptions) (Replica, string, error) { func (db *DB) CalcRestoreTarget(ctx context.Context, opt RestoreOptions) (Replica, string, error) {
var target struct { var target struct {
@@ -1613,8 +1689,10 @@ func restoreSnapshot(ctx context.Context, r Replica, generation string, index in
return f.Close() return f.Close()
} }
// restoreWAL copies a WAL file from the replica to the local WAL and forces checkpoint. // downloadWAL copies a WAL file from the replica to a local copy next to the DB.
func restoreWAL(ctx context.Context, r Replica, generation string, index int, dbPath string) error { // The WAL is later applied by applyWAL(). This function can be run in parallel
// to download multiple WAL files simultaneously.
func downloadWAL(ctx context.Context, r Replica, generation string, index int, dbPath string) error {
// Determine the user/group & mode based on the DB, if available. // Determine the user/group & mode based on the DB, if available.
uid, gid, mode := -1, -1, os.FileMode(0600) uid, gid, mode := -1, -1, os.FileMode(0600)
if db := r.DB(); db != nil { if db := r.DB(); db != nil {
@@ -1629,7 +1707,7 @@ func restoreWAL(ctx context.Context, r Replica, generation string, index int, db
defer rd.Close() defer rd.Close()
// Open handle to destination WAL path. // Open handle to destination WAL path.
f, err := createFile(dbPath+"-wal", mode, uid, gid) f, err := createFile(fmt.Sprintf("%s-%08x-wal", dbPath, index), mode, uid, gid)
if err != nil { if err != nil {
return err return err
} }
@@ -1641,6 +1719,15 @@ func restoreWAL(ctx context.Context, r Replica, generation string, index int, db
} else if err := f.Close(); err != nil { } else if err := f.Close(); err != nil {
return err return err
} }
return nil
}
// applyWAL performs a truncating checkpoint on the given database.
func applyWAL(ctx context.Context, index int, dbPath string) error {
// Copy WAL file from it's staging path to the correct "-wal" location.
if err := os.Rename(fmt.Sprintf("%s-%08x-wal", dbPath, index), dbPath+"-wal"); err != nil {
return err
}
// Open SQLite database and force a truncating checkpoint. // Open SQLite database and force a truncating checkpoint.
d, err := sql.Open("sqlite3", dbPath) d, err := sql.Open("sqlite3", dbPath)
@@ -1655,7 +1742,6 @@ func restoreWAL(ctx context.Context, r Replica, generation string, index int, db
} else if row[0] != 0 { } else if row[0] != 0 {
return fmt.Errorf("truncation checkpoint failed during restore (%d,%d,%d)", row[0], row[1], row[2]) return fmt.Errorf("truncation checkpoint failed during restore (%d,%d,%d)", row[0], row[1], row[2])
} }
return d.Close() return d.Close()
} }
@@ -1666,7 +1752,7 @@ func restoreWAL(ctx context.Context, r Replica, generation string, index int, db
// unable to checkpoint during this time. // unable to checkpoint during this time.
// //
// If dst is set, the database file is copied to that location before checksum. // If dst is set, the database file is copied to that location before checksum.
func (db *DB) CRC64() (uint64, Pos, error) { func (db *DB) CRC64(ctx context.Context) (uint64, Pos, error) {
db.mu.Lock() db.mu.Lock()
defer db.mu.Unlock() defer db.mu.Unlock()
@@ -1684,7 +1770,7 @@ func (db *DB) CRC64() (uint64, Pos, error) {
} }
// Force a RESTART checkpoint to ensure the database is at the start of the WAL. // Force a RESTART checkpoint to ensure the database is at the start of the WAL.
if err := db.checkpointAndInit(generation, CheckpointModeRestart); err != nil { if err := db.checkpointAndInit(ctx, generation, CheckpointModeRestart); err != nil {
return 0, Pos{}, err return 0, Pos{}, err
} }
@@ -1706,6 +1792,9 @@ func (db *DB) CRC64() (uint64, Pos, error) {
return h.Sum64(), pos, nil return h.Sum64(), pos, nil
} }
// DefaultRestoreParallelism is the default parallelism when downloading WAL files.
const DefaultRestoreParallelism = 8
// RestoreOptions represents options for DB.Restore(). // RestoreOptions represents options for DB.Restore().
type RestoreOptions struct { type RestoreOptions struct {
// Target path to restore into. // Target path to restore into.
@@ -1721,16 +1810,15 @@ type RestoreOptions struct {
Generation string Generation string
// Specific index to restore from. // Specific index to restore from.
// Set to math.MaxInt64 to ignore index. // Set to math.MaxInt32 to ignore index.
Index int Index int
// Point-in-time to restore database. // Point-in-time to restore database.
// If zero, database restore to most recent state available. // If zero, database restore to most recent state available.
Timestamp time.Time Timestamp time.Time
// If true, no actual restore is performed. // Specifies how many WAL files are downloaded in parallel during restore.
// Only equivalent log output for a regular restore. Parallelism int
DryRun bool
// Logging settings. // Logging settings.
Logger *log.Logger Logger *log.Logger
@@ -1740,7 +1828,8 @@ type RestoreOptions struct {
// NewRestoreOptions returns a new instance of RestoreOptions with defaults. // NewRestoreOptions returns a new instance of RestoreOptions with defaults.
func NewRestoreOptions() RestoreOptions { func NewRestoreOptions() RestoreOptions {
return RestoreOptions{ return RestoreOptions{
Index: math.MaxInt64, Index: math.MaxInt32,
Parallelism: DefaultRestoreParallelism,
} }
} }

View File

@@ -1,10 +1,12 @@
package litestream_test package litestream_test
import ( import (
"context"
"database/sql" "database/sql"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"time" "time"
@@ -98,9 +100,12 @@ func TestDB_UpdatedAt(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
sleepTime := 100 * time.Millisecond
if os.Getenv("CI") != "" { if os.Getenv("CI") != "" {
time.Sleep(1 * time.Second) sleepTime = 1 * time.Second
} }
time.Sleep(sleepTime)
if _, err := sqldb.Exec(`CREATE TABLE t (id INT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE t (id INT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -118,7 +123,7 @@ func TestDB_CRC64(t *testing.T) {
t.Run("ErrNotExist", func(t *testing.T) { t.Run("ErrNotExist", func(t *testing.T) {
db := MustOpenDB(t) db := MustOpenDB(t)
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
if _, _, err := db.CRC64(); !os.IsNotExist(err) { if _, _, err := db.CRC64(context.Background()); !os.IsNotExist(err) {
t.Fatalf("unexpected error: %#v", err) t.Fatalf("unexpected error: %#v", err)
} }
}) })
@@ -127,11 +132,11 @@ func TestDB_CRC64(t *testing.T) {
db, sqldb := MustOpenDBs(t) db, sqldb := MustOpenDBs(t)
defer MustCloseDBs(t, db, sqldb) defer MustCloseDBs(t, db, sqldb)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
chksum0, _, err := db.CRC64() chksum0, _, err := db.CRC64(context.Background())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -139,7 +144,7 @@ func TestDB_CRC64(t *testing.T) {
// Issue change that is applied to the WAL. Checksum should not change. // Issue change that is applied to the WAL. Checksum should not change.
if _, err := sqldb.Exec(`CREATE TABLE t (id INT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE t (id INT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if chksum1, _, err := db.CRC64(); err != nil { } else if chksum1, _, err := db.CRC64(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if chksum0 == chksum1 { } else if chksum0 == chksum1 {
t.Fatal("expected different checksum event after WAL change") t.Fatal("expected different checksum event after WAL change")
@@ -150,7 +155,7 @@ func TestDB_CRC64(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if chksum2, _, err := db.CRC64(); err != nil { if chksum2, _, err := db.CRC64(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if chksum0 == chksum2 { } else if chksum0 == chksum2 {
t.Fatal("expected different checksums after checkpoint") t.Fatal("expected different checksums after checkpoint")
@@ -164,7 +169,7 @@ func TestDB_Sync(t *testing.T) {
t.Run("NoDB", func(t *testing.T) { t.Run("NoDB", func(t *testing.T) {
db := MustOpenDB(t) db := MustOpenDB(t)
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
}) })
@@ -174,7 +179,7 @@ func TestDB_Sync(t *testing.T) {
db, sqldb := MustOpenDBs(t) db, sqldb := MustOpenDBs(t)
defer MustCloseDBs(t, db, sqldb) defer MustCloseDBs(t, db, sqldb)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -212,7 +217,7 @@ func TestDB_Sync(t *testing.T) {
} }
// Perform initial sync & grab initial position. // Perform initial sync & grab initial position.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -227,7 +232,7 @@ func TestDB_Sync(t *testing.T) {
} }
// Sync to ensure position moves forward one page. // Sync to ensure position moves forward one page.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if pos1, err := db.Pos(); err != nil { } else if pos1, err := db.Pos(); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -246,7 +251,7 @@ func TestDB_Sync(t *testing.T) {
defer MustCloseDBs(t, db, sqldb) defer MustCloseDBs(t, db, sqldb)
// Issue initial sync and truncate WAL. // Issue initial sync and truncate WAL.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -275,7 +280,7 @@ func TestDB_Sync(t *testing.T) {
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
// Re-sync and ensure new generation has been created. // Re-sync and ensure new generation has been created.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -298,7 +303,7 @@ func TestDB_Sync(t *testing.T) {
} }
// Issue initial sync and truncate WAL. // Issue initial sync and truncate WAL.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -334,7 +339,7 @@ func TestDB_Sync(t *testing.T) {
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
// Re-sync and ensure new generation has been created. // Re-sync and ensure new generation has been created.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -354,7 +359,7 @@ func TestDB_Sync(t *testing.T) {
// Execute a query to force a write to the WAL and then sync. // Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -378,7 +383,7 @@ func TestDB_Sync(t *testing.T) {
// Reopen managed database & ensure sync will still work. // Reopen managed database & ensure sync will still work.
db = MustOpenDBAt(t, db.Path()) db = MustOpenDBAt(t, db.Path())
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -398,7 +403,7 @@ func TestDB_Sync(t *testing.T) {
// Execute a query to force a write to the WAL and then sync. // Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -417,7 +422,7 @@ func TestDB_Sync(t *testing.T) {
// Reopen managed database & ensure sync will still work. // Reopen managed database & ensure sync will still work.
db = MustOpenDBAt(t, db.Path()) db = MustOpenDBAt(t, db.Path())
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -437,7 +442,7 @@ func TestDB_Sync(t *testing.T) {
// Execute a query to force a write to the WAL and then sync. // Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -462,7 +467,7 @@ func TestDB_Sync(t *testing.T) {
// Reopen managed database & ensure sync will still work. // Reopen managed database & ensure sync will still work.
db = MustOpenDBAt(t, db.Path()) db = MustOpenDBAt(t, db.Path())
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -489,7 +494,7 @@ func TestDB_Sync(t *testing.T) {
// Execute a query to force a write to the WAL and then sync. // Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -508,7 +513,7 @@ func TestDB_Sync(t *testing.T) {
// Reopen managed database & ensure sync will still work. // Reopen managed database & ensure sync will still work.
db = MustOpenDBAt(t, db.Path()) db = MustOpenDBAt(t, db.Path())
defer MustCloseDB(t, db) defer MustCloseDB(t, db)
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -532,7 +537,7 @@ func TestDB_Sync(t *testing.T) {
// Execute a query to force a write to the WAL and then sync. // Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -544,7 +549,7 @@ func TestDB_Sync(t *testing.T) {
} }
// Sync to shadow WAL. // Sync to shadow WAL.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -564,7 +569,7 @@ func TestDB_Sync(t *testing.T) {
// Execute a query to force a write to the WAL and then sync. // Execute a query to force a write to the WAL and then sync.
if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil { if _, err := sqldb.Exec(`CREATE TABLE foo (bar TEXT);`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -574,7 +579,7 @@ func TestDB_Sync(t *testing.T) {
// Write to WAL & sync. // Write to WAL & sync.
if _, err := sqldb.Exec(`INSERT INTO foo (bar) VALUES ('baz');`); err != nil { if _, err := sqldb.Exec(`INSERT INTO foo (bar) VALUES ('baz');`); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := db.Sync(); err != nil { } else if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -589,12 +594,14 @@ func TestDB_Sync(t *testing.T) {
// MustOpenDBs returns a new instance of a DB & associated SQL DB. // MustOpenDBs returns a new instance of a DB & associated SQL DB.
func MustOpenDBs(tb testing.TB) (*litestream.DB, *sql.DB) { func MustOpenDBs(tb testing.TB) (*litestream.DB, *sql.DB) {
tb.Helper()
db := MustOpenDB(tb) db := MustOpenDB(tb)
return db, MustOpenSQLDB(tb, db.Path()) return db, MustOpenSQLDB(tb, db.Path())
} }
// MustCloseDBs closes db & sqldb and removes the parent directory. // MustCloseDBs closes db & sqldb and removes the parent directory.
func MustCloseDBs(tb testing.TB, db *litestream.DB, sqldb *sql.DB) { func MustCloseDBs(tb testing.TB, db *litestream.DB, sqldb *sql.DB) {
tb.Helper()
MustCloseDB(tb, db) MustCloseDB(tb, db)
MustCloseSQLDB(tb, sqldb) MustCloseSQLDB(tb, sqldb)
} }
@@ -619,7 +626,7 @@ func MustOpenDBAt(tb testing.TB, path string) *litestream.DB {
// MustCloseDB closes db and removes its parent directory. // MustCloseDB closes db and removes its parent directory.
func MustCloseDB(tb testing.TB, db *litestream.DB) { func MustCloseDB(tb testing.TB, db *litestream.DB) {
tb.Helper() tb.Helper()
if err := db.Close(); err != nil { if err := db.Close(); err != nil && !strings.Contains(err.Error(), `database is closed`) {
tb.Fatal(err) tb.Fatal(err)
} else if err := os.RemoveAll(filepath.Dir(db.Path())); err != nil { } else if err := os.RemoveAll(filepath.Dir(db.Path())); err != nil {
tb.Fatal(err) tb.Fatal(err)

View File

@@ -1,13 +1,13 @@
name: litestream name: litestream
arch: amd64 arch: "${GOARCH}"
platform: linux platform: "${GOOS}"
version: "${LITESTREAM_VERSION}" version: "${LITESTREAM_VERSION}"
section: "default" section: "default"
priority: "extra" priority: "extra"
maintainer: "Ben Johnson <benbjohnson@yahoo.com>" maintainer: "Ben Johnson <benbjohnson@yahoo.com>"
description: Litestream is a tool for real-time replication of SQLite databases. description: Litestream is a tool for real-time replication of SQLite databases.
homepage: "https://github.com/benbjohnson/litestream" homepage: "https://github.com/benbjohnson/litestream"
license: "GPLv3" license: "Apache 2"
contents: contents:
- src: ./litestream - src: ./litestream
dst: /usr/bin/litestream dst: /usr/bin/litestream

1
go.mod
View File

@@ -8,6 +8,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.5 github.com/mattn/go-sqlite3 v1.14.5
github.com/pierrec/lz4/v4 v4.1.3 github.com/pierrec/lz4/v4 v4.1.3
github.com/prometheus/client_golang v1.9.0 github.com/prometheus/client_golang v1.9.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
) )

2
go.sum
View File

@@ -316,6 +316,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

View File

@@ -37,6 +37,9 @@ type Replica interface {
// Stops all replication processing. Blocks until processing stopped. // Stops all replication processing. Blocks until processing stopped.
Stop(hard bool) error Stop(hard bool) error
// Performs a backup of outstanding WAL frames to the replica.
Sync(ctx context.Context) error
// Returns the last replication position. // Returns the last replication position.
LastPos() Pos LastPos() Pos
@@ -604,6 +607,11 @@ func (r *FileReplica) snapshot(ctx context.Context, generation string, index int
r.muf.Lock() r.muf.Lock()
defer r.muf.Unlock() defer r.muf.Unlock()
// Issue a passive checkpoint to flush any pages to disk before snapshotting.
if _, err := r.db.db.ExecContext(ctx, `PRAGMA wal_checkpoint(PASSIVE);`); err != nil {
return fmt.Errorf("pre-snapshot checkpoint: %w", err)
}
// Acquire a read lock on the database during snapshot to prevent checkpoints. // Acquire a read lock on the database during snapshot to prevent checkpoints.
tx, err := r.db.db.Begin() tx, err := r.db.db.Begin()
if err != nil { if err != nil {
@@ -1061,34 +1069,65 @@ func SnapshotIndexAt(ctx context.Context, r Replica, generation string, timestam
return 0, ErrNoSnapshots return 0, ErrNoSnapshots
} }
index := -1 snapshotIndex := -1
var max time.Time var max time.Time
for _, snapshot := range snapshots { for _, snapshot := range snapshots {
if !timestamp.IsZero() && snapshot.CreatedAt.After(timestamp) { if snapshot.Generation != generation {
continue // generation mismatch, skip
} else if !timestamp.IsZero() && snapshot.CreatedAt.After(timestamp) {
continue // after timestamp, skip continue // after timestamp, skip
} }
// Use snapshot if it newer. // Use snapshot if it newer.
if max.IsZero() || snapshot.CreatedAt.After(max) { if max.IsZero() || snapshot.CreatedAt.After(max) {
index, max = snapshot.Index, snapshot.CreatedAt snapshotIndex, max = snapshot.Index, snapshot.CreatedAt
} }
} }
if index == -1 { if snapshotIndex == -1 {
return 0, ErrNoSnapshots return 0, ErrNoSnapshots
} }
return index, nil return snapshotIndex, nil
} }
// WALIndexAt returns the highest index for a WAL file that occurs before maxIndex & timestamp. // SnapshotIndexbyIndex returns the highest index for a snapshot within a generation
// If timestamp is zero, returns the highest WAL index. // that occurs before a given index. If index is MaxInt32, returns the latest snapshot.
func SnapshotIndexByIndex(ctx context.Context, r Replica, generation string, index int) (int, error) {
snapshots, err := r.Snapshots(ctx)
if err != nil {
return 0, err
} else if len(snapshots) == 0 {
return 0, ErrNoSnapshots
}
snapshotIndex := -1
for _, snapshot := range snapshots {
if index < math.MaxInt32 && snapshot.Index > index {
continue // after index, skip
}
// Use snapshot if it newer.
if snapshotIndex == -1 || snapshotIndex >= snapshotIndex {
snapshotIndex = snapshot.Index
}
}
if snapshotIndex == -1 {
return 0, ErrNoSnapshots
}
return snapshotIndex, nil
}
// WALIndexAt returns the highest index for a WAL file that occurs before
// maxIndex & timestamp. If timestamp is zero, returns the highest WAL index.
// Returns -1 if no WAL found and MaxInt32 specified.
func WALIndexAt(ctx context.Context, r Replica, generation string, maxIndex int, timestamp time.Time) (int, error) { func WALIndexAt(ctx context.Context, r Replica, generation string, maxIndex int, timestamp time.Time) (int, error) {
wals, err := r.WALs(ctx) wals, err := r.WALs(ctx)
if err != nil { if err != nil {
return 0, err return 0, err
} }
var index int index := -1
for _, wal := range wals { for _, wal := range wals {
if wal.Generation != generation { if wal.Generation != generation {
continue continue
@@ -1106,7 +1145,7 @@ func WALIndexAt(ctx context.Context, r Replica, generation string, maxIndex int,
} }
// If max index is specified but not found, return an error. // If max index is specified but not found, return an error.
if maxIndex != math.MaxInt64 && index != maxIndex { if maxIndex != math.MaxInt32 && index != maxIndex {
return index, fmt.Errorf("unable to locate index %d in generation %q, highest index was %d", maxIndex, generation, index) return index, fmt.Errorf("unable to locate index %d in generation %q, highest index was %d", maxIndex, generation, index)
} }
return index, nil return index, nil
@@ -1164,7 +1203,7 @@ func ValidateReplica(ctx context.Context, r Replica) error {
// Compute checksum of primary database under lock. This prevents a // Compute checksum of primary database under lock. This prevents a
// sync from occurring and the database will not be written. // sync from occurring and the database will not be written.
chksum0, pos, err := db.CRC64() chksum0, pos, err := db.CRC64(ctx)
if err != nil { if err != nil {
return fmt.Errorf("cannot compute checksum: %w", err) return fmt.Errorf("cannot compute checksum: %w", err)
} }

View File

@@ -15,7 +15,7 @@ func TestFileReplica_Sync(t *testing.T) {
r := NewTestFileReplica(t, db) r := NewTestFileReplica(t, db)
// Sync database & then sync replica. // Sync database & then sync replica.
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := r.Sync(context.Background()); err != nil { } else if err := r.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -47,7 +47,7 @@ func TestFileReplica_Sync(t *testing.T) {
// Sync periodically. // Sync periodically.
if i%100 == 0 || i == n-1 { if i%100 == 0 || i == n-1 {
if err := db.Sync(); err != nil { if err := db.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)
} else if err := r.Sync(context.Background()); err != nil { } else if err := r.Sync(context.Background()); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@@ -3,11 +3,13 @@ package s3
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/tls"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"net/http"
"os" "os"
"path" "path"
"regexp" "regexp"
@@ -79,6 +81,7 @@ type Replica struct {
Path string Path string
Endpoint string Endpoint string
ForcePathStyle bool ForcePathStyle bool
SkipVerify bool
// Time between syncs with the shadow WAL. // Time between syncs with the shadow WAL.
SyncInterval time.Duration SyncInterval time.Duration
@@ -646,6 +649,11 @@ func (r *Replica) snapshot(ctx context.Context, generation string, index int) er
r.muf.Lock() r.muf.Lock()
defer r.muf.Unlock() defer r.muf.Unlock()
// Issue a passive checkpoint to flush any pages to disk before snapshotting.
if _, err := r.db.SQLDB().ExecContext(ctx, `PRAGMA wal_checkpoint(PASSIVE);`); err != nil {
return fmt.Errorf("pre-snapshot checkpoint: %w", err)
}
// Acquire a read lock on the database during snapshot to prevent checkpoints. // Acquire a read lock on the database during snapshot to prevent checkpoints.
tx, err := r.db.SQLDB().Begin() tx, err := r.db.SQLDB().Begin()
if err != nil { if err != nil {
@@ -769,6 +777,12 @@ func (r *Replica) config() *aws.Config {
if r.ForcePathStyle { if r.ForcePathStyle {
config.S3ForcePathStyle = aws.Bool(r.ForcePathStyle) config.S3ForcePathStyle = aws.Bool(r.ForcePathStyle)
} }
if r.SkipVerify {
config.HTTPClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
}
return config return config
} }