From fec0337630c07a0938e6f93211d7a41b416ec025 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 12:16:08 -0600 Subject: [PATCH 01/19] add draft trial version of molecula --- Makefile | 11 ++++++++-- go.mod | 1 + go.sum | 2 ++ server/server.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 3 ++- 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2e6cc7331..8a724c282 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice +.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice release-build-trial CLONE_URL=github.com/pilosa/pilosa MOD_VERSION=v2 @@ -11,7 +11,7 @@ BRANCH_ID := $(BRANCH)-$(GOOS)-$(GOARCH) BUILD_TIME := $(shell date -u +%FT%T%z) SHARD_WIDTH = 20 COMMIT := $(shell git describe --exact-match >/dev/null 2>&1 || git rev-parse --short HEAD) -LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT)" +LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT) -X github.com/pilosa/pilosa/v2.TrialDeadline=$(TRIAL_DEADLINE)" GO_VERSION=1.14.10 RELEASE ?= 0 RELEASE_ENABLED = $(subst 0,,$(RELEASE)) @@ -98,6 +98,13 @@ cover-viz: cover build: go build -tags='$(BUILD_TAGS)' -ldflags $(LDFLAGS) $(FLAGS) ./cmd/pilosa +# Create a single release trial build under the build directory +release-build-trial: + $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-trial-$(VERSION_ID)/pilosa" RELEASE=1 + cp NOTICE README.md build/pilosa-trial-$(VERSION_ID) + tar -cvz -C build -f build/pilosa-trial-$(VERSION_ID).tar.gz pilosa-trial-$(VERSION_ID)/ + @echo Created release trial build: build/pilosa-trial-$(VERSION_ID).tar.gz + # Create a single release build under the build directory release-build: $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-$(VERSION_ID)/pilosa" RELEASE=1 diff --git a/go.mod b/go.mod index f0d161c95..dcd6b603a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ replace github.com/hashicorp/memberlist => github.com/pilosa/memberlist v0.1.4-0 require ( github.com/CAFxX/gcnotifier v0.0.0-20190112062741-224a280d589d github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895 + github.com/beevik/ntp v0.3.0 github.com/benbjohnson/immutable v0.3.0 github.com/cespare/xxhash v1.1.0 github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect diff --git a/go.sum b/go.sum index b9d5e44d7..f1ad4ecc5 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZFFEjBj46YV4rDjvGrNxb0KMWYkL2I= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/beevik/ntp v0.3.0 h1:xzVrPrE4ziasFXgBVBZJDP0Wg/KpMwk2KHJ4Ba8GrDw= +github.com/beevik/ntp v0.3.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg= github.com/benbjohnson/immutable v0.3.0 h1:TVRhuZx2wG9SZ0LRdqlbs9S5BZ6Y24hJEHTCgWHZEIw= github.com/benbjohnson/immutable v0.3.0/go.mod h1:uc6OHo6PN2++n98KHLxW8ef4W42ylHiQSENghE1ezxI= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= diff --git a/server/server.go b/server/server.go index 530e4102a..1e9a8d053 100644 --- a/server/server.go +++ b/server/server.go @@ -39,6 +39,7 @@ import ( "golang.org/x/sync/errgroup" + "github.com/beevik/ntp" "github.com/pelletier/go-toml" "github.com/pilosa/pilosa/v2" "github.com/pilosa/pilosa/v2/boltdb" @@ -285,6 +286,16 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) + if pilosa.TrialDeadline != "" { + layout := "2006-02-02" + pilosa.TrialDeadline = "2021-02-06" + endTime, err := time.Parse(layout, pilosa.TrialDeadline) + if err != nil { + return errors.Wrap(err, "parsing curTime from make file") + } + go m.dailyCheck(endTime) + } + // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct // the environment, so that pilosa/txfactory.go can determine the @@ -478,6 +489,48 @@ func (m *Command) SetupServer() error { return errors.Wrap(err, "new handler") } +// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days +func (m *Command) dailyCheck(end time.Time) error { + ticker := time.NewTicker(5 * time.Second) + var err error + for range ticker.C { + cur, err := m.ntpServerTime() + if err != nil { + return errors.Wrap(err, "reading ntp server time") + } + + m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) + if end.Sub(cur) <= 0 { + m.logger.Printf("end: %v", end) // test line + m.logger.Printf("cur: %v", cur) // test line + m.logger.Printf("Your free trial has ended") + os.Exit(0) //is 0 the right exit number? + } + } + return errors.Wrap(err, "reading ntp server time") +} + +// ntpServerTime attempts to reach ntp servers with delays between each attempt +func (m *Command) ntpServerTime() (time.Time, error) { + curTime, err := ntp.Time("0.beevik-ntp.pool.ntp.org") + errCount := 0 + if err != nil { + for i := 0; i < 4; i++ { + curTime, err = ntp.Time("0.beevik-ntp.pool.ntp.org") + if err != nil { + errCount++ + if errCount >= 4 { + return curTime, err + } + time.Sleep(100 * time.Millisecond) + } else { + break + } + } + } + return curTime, err +} + // setupNetworking sets up internode communication based on the configuration. func (m *Command) setupNetworking() error { if m.Config.Cluster.Disabled { diff --git a/version.go b/version.go index 91a5841b7..7c3f2361b 100644 --- a/version.go +++ b/version.go @@ -25,6 +25,7 @@ var Variant string var BuildTime string var LatticeCommit string var GoVersion string = runtime.Version() +var TrialDeadline string func VersionInfo() string { var prefix string @@ -52,7 +53,7 @@ func VersionInfo() string { case buildTime != "": suffix += " (" + buildTime + ")" } - suffix += " " + GoVersion + suffix += " " + GoVersion + " limited time trial ends at: " + TrialDeadline return prefix + "Pilosa" + suffix } From fb47cb875a620cb466ee418fc593ba834d2c8332 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 13:40:57 -0600 Subject: [PATCH 02/19] remove error return type --- server/server.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/server.go b/server/server.go index 1e9a8d053..7ce26a8d0 100644 --- a/server/server.go +++ b/server/server.go @@ -490,13 +490,14 @@ func (m *Command) SetupServer() error { } // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(end time.Time) error { +func (m *Command) dailyCheck(end time.Time) { ticker := time.NewTicker(5 * time.Second) var err error for range ticker.C { cur, err := m.ntpServerTime() if err != nil { - return errors.Wrap(err, "reading ntp server time") + errors.Wrap(err, "reading ntp server time") + os.Exit(1) } m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) @@ -507,7 +508,8 @@ func (m *Command) dailyCheck(end time.Time) error { os.Exit(0) //is 0 the right exit number? } } - return errors.Wrap(err, "reading ntp server time") + errors.Wrap(err, "reading ntp server time") + os.Exit(1) } // ntpServerTime attempts to reach ntp servers with delays between each attempt From f37ec5b425f6596c8e2990ed14be6e16c9cf2c98 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 14:25:57 -0600 Subject: [PATCH 03/19] alter error output to satisfy test --- server/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/server.go b/server/server.go index 7ce26a8d0..3e416436e 100644 --- a/server/server.go +++ b/server/server.go @@ -496,7 +496,7 @@ func (m *Command) dailyCheck(end time.Time) { for range ticker.C { cur, err := m.ntpServerTime() if err != nil { - errors.Wrap(err, "reading ntp server time") + m.logger.Printf("reading ntp server time %v", err) os.Exit(1) } @@ -508,7 +508,7 @@ func (m *Command) dailyCheck(end time.Time) { os.Exit(0) //is 0 the right exit number? } } - errors.Wrap(err, "reading ntp server time") + m.logger.Printf("reading ntp server time %v", err) os.Exit(1) } From c967bc96f4c5c782b1300635593e8009eefb2010 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 15:40:55 -0600 Subject: [PATCH 04/19] remove test code --- server/server.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/server/server.go b/server/server.go index 3e416436e..089d29dba 100644 --- a/server/server.go +++ b/server/server.go @@ -287,8 +287,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) if pilosa.TrialDeadline != "" { - layout := "2006-02-02" - pilosa.TrialDeadline = "2021-02-06" + layout := "2006-01-02" endTime, err := time.Parse(layout, pilosa.TrialDeadline) if err != nil { return errors.Wrap(err, "parsing curTime from make file") @@ -499,13 +498,10 @@ func (m *Command) dailyCheck(end time.Time) { m.logger.Printf("reading ntp server time %v", err) os.Exit(1) } - m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) if end.Sub(cur) <= 0 { - m.logger.Printf("end: %v", end) // test line - m.logger.Printf("cur: %v", cur) // test line m.logger.Printf("Your free trial has ended") - os.Exit(0) //is 0 the right exit number? + os.Exit(0) } } m.logger.Printf("reading ntp server time %v", err) From 7ae1599c12e777405b8b0940abb6ded7056f6dd9 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Tue, 9 Feb 2021 15:39:32 -0600 Subject: [PATCH 05/19] edit and move trial code from server.go to trial.go and combine release build targets --- Makefile | 16 ++++--------- server/server.go | 51 +--------------------------------------- server/trial.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 server/trial.go diff --git a/Makefile b/Makefile index 8a724c282..e66f44249 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ BUILD_TIME := $(shell date -u +%FT%T%z) SHARD_WIDTH = 20 COMMIT := $(shell git describe --exact-match >/dev/null 2>&1 || git rev-parse --short HEAD) LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT) -X github.com/pilosa/pilosa/v2.TrialDeadline=$(TRIAL_DEADLINE)" +TRIAL_STRING = $(if $(TRIAL_DEADLINE),"-trial-$(TRIAL_DEADLINE)","") GO_VERSION=1.14.10 RELEASE ?= 0 RELEASE_ENABLED = $(subst 0,,$(RELEASE)) @@ -98,19 +99,12 @@ cover-viz: cover build: go build -tags='$(BUILD_TAGS)' -ldflags $(LDFLAGS) $(FLAGS) ./cmd/pilosa -# Create a single release trial build under the build directory -release-build-trial: - $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-trial-$(VERSION_ID)/pilosa" RELEASE=1 - cp NOTICE README.md build/pilosa-trial-$(VERSION_ID) - tar -cvz -C build -f build/pilosa-trial-$(VERSION_ID).tar.gz pilosa-trial-$(VERSION_ID)/ - @echo Created release trial build: build/pilosa-trial-$(VERSION_ID).tar.gz - # Create a single release build under the build directory release-build: - $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-$(VERSION_ID)/pilosa" RELEASE=1 - cp NOTICE README.md LICENSE build/pilosa-$(VERSION_ID) - tar -cvz -C build -f build/pilosa-$(VERSION_ID).tar.gz pilosa-$(VERSION_ID)/ - @echo Created release build: build/pilosa-$(VERSION_ID).tar.gz + $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa$(TRIAL_STRING)-$(VERSION_ID)/pilosa" RELEASE=1 + cp NOTICE README.md LICENSE build/pilosa$(TRIAL_STRING)-$(VERSION_ID) + tar -cvz -C build -f build/pilosa$(TRIAL_STRING)-$(VERSION_ID).tar.gz pilosa$(TRIAL_STRING)-$(VERSION_ID)/ + @echo Created release build: build/pilosa$(TRIAL_STRING)-$(VERSION_ID).tar.gz # Error out if there are untracked changes in Git check-clean: diff --git a/server/server.go b/server/server.go index 089d29dba..cbd70a99f 100644 --- a/server/server.go +++ b/server/server.go @@ -39,7 +39,6 @@ import ( "golang.org/x/sync/errgroup" - "github.com/beevik/ntp" "github.com/pelletier/go-toml" "github.com/pilosa/pilosa/v2" "github.com/pilosa/pilosa/v2/boltdb" @@ -286,14 +285,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) - if pilosa.TrialDeadline != "" { - layout := "2006-01-02" - endTime, err := time.Parse(layout, pilosa.TrialDeadline) - if err != nil { - return errors.Wrap(err, "parsing curTime from make file") - } - go m.dailyCheck(endTime) - } + m.trialVersion() // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct @@ -488,47 +480,6 @@ func (m *Command) SetupServer() error { return errors.Wrap(err, "new handler") } -// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(end time.Time) { - ticker := time.NewTicker(5 * time.Second) - var err error - for range ticker.C { - cur, err := m.ntpServerTime() - if err != nil { - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) - } - m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) - if end.Sub(cur) <= 0 { - m.logger.Printf("Your free trial has ended") - os.Exit(0) - } - } - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) -} - -// ntpServerTime attempts to reach ntp servers with delays between each attempt -func (m *Command) ntpServerTime() (time.Time, error) { - curTime, err := ntp.Time("0.beevik-ntp.pool.ntp.org") - errCount := 0 - if err != nil { - for i := 0; i < 4; i++ { - curTime, err = ntp.Time("0.beevik-ntp.pool.ntp.org") - if err != nil { - errCount++ - if errCount >= 4 { - return curTime, err - } - time.Sleep(100 * time.Millisecond) - } else { - break - } - } - } - return curTime, err -} - // setupNetworking sets up internode communication based on the configuration. func (m *Command) setupNetworking() error { if m.Config.Cluster.Disabled { diff --git a/server/trial.go b/server/trial.go new file mode 100644 index 000000000..85df72422 --- /dev/null +++ b/server/trial.go @@ -0,0 +1,61 @@ +package server + +import ( + "os" + "time" + + "github.com/beevik/ntp" + "github.com/pilosa/pilosa/v2" +) + +func (m *Command) trialVersion() { + if pilosa.TrialDeadline != "" { + endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + if err != nil { + m.logger.Printf("parsing curTime from make file: %v", err) + os.Exit(1) + } + go m.dailyCheck(endTime) + } +} + +const hoursPerCheck = 24 * time.Hour + +// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days +func (m *Command) dailyCheck(endTime time.Time) { + ticker := time.NewTicker(hoursPerCheck) + startTime, err := m.ntpServerTime(4) + if err != nil { + m.logger.Printf("reading ntp server time %v", err) + os.Exit(1) + } + runDuration := endTime.Sub(startTime) + if runDuration <= 0 { + m.logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) + } + for range ticker.C { + runningDuration := time.Since(startTime) + m.logger.Printf("Current time remaining in trial: %v", runDuration-runningDuration) + if runningDuration >= runDuration { + m.logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) + } + } +} + +const url = "0.beevik-ntp.pool.ntp.org" +const timeRequestDelay = 100 * time.Millisecond + +// ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server +func (m *Command) ntpServerTime(retries int) (time.Time, error) { + t, err := ntp.Time(url) + if err != nil && retries <= 0 { + return t, err + } + if err != nil { + time.Sleep(timeRequestDelay) + return m.ntpServerTime(retries - 1) + } + return t, nil +} From 4bcdfcc9a2b81ad7d57cb9153eb50e196db4386e Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Wed, 10 Feb 2021 08:21:36 -0600 Subject: [PATCH 06/19] remove remaining release-build-trial --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e66f44249..0f06e1b92 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice release-build-trial +.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice CLONE_URL=github.com/pilosa/pilosa MOD_VERSION=v2 From 302093035edefe8011ab736c86a53d5777983e7e Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Wed, 10 Feb 2021 12:37:13 -0600 Subject: [PATCH 07/19] add licence header --- server/trial.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/server/trial.go b/server/trial.go index 85df72422..8feb6bfa4 100644 --- a/server/trial.go +++ b/server/trial.go @@ -1,3 +1,22 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Package server contains the `pilosa server` subcommand which runs Pilosa +// itself. The purpose of this package is to define an easily tested Command +// object which handles interpreting configuration and setting up all the +// objects that Pilosa needs. + package server import ( @@ -12,18 +31,18 @@ func (m *Command) trialVersion() { if pilosa.TrialDeadline != "" { endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) if err != nil { - m.logger.Printf("parsing curTime from make file: %v", err) + m.logger.Printf("parsing trial deadline: %v", err) os.Exit(1) } go m.dailyCheck(endTime) } } -const hoursPerCheck = 24 * time.Hour +const trialCheckInterval = 24 * time.Hour // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days func (m *Command) dailyCheck(endTime time.Time) { - ticker := time.NewTicker(hoursPerCheck) + ticker := time.NewTicker(trialCheckInterval) startTime, err := m.ntpServerTime(4) if err != nil { m.logger.Printf("reading ntp server time %v", err) @@ -44,17 +63,17 @@ func (m *Command) dailyCheck(endTime time.Time) { } } -const url = "0.beevik-ntp.pool.ntp.org" -const timeRequestDelay = 100 * time.Millisecond +const ntpurl = "0.beevik-ntp.pool.ntp.org" +const ntpRetryDelay = 100 * time.Millisecond // ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server func (m *Command) ntpServerTime(retries int) (time.Time, error) { - t, err := ntp.Time(url) + t, err := ntp.Time(ntpurl) if err != nil && retries <= 0 { return t, err } if err != nil { - time.Sleep(timeRequestDelay) + time.Sleep(ntpRetryDelay) return m.ntpServerTime(retries - 1) } return t, nil From 0f952bb5948ac289af168160729f8632996e6132 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Thu, 11 Feb 2021 10:36:04 -0600 Subject: [PATCH 08/19] Respond to code review feedback change logic in version.go so that the trial related messages only appear on trial versions of molecula convert Command methods in trial.go to functions and pass a loggerLogger variable instead since that was the only piece of Command being used add a function named expireAfter which seperately runs similar functionality to what was previously in daily check with chnages directed at stopping users from changing their internal clock date change variable names and placement to be more readable and organized --- server/server.go | 2 +- server/trial.go | 54 +++++++++++++++++++++++++----------------------- version.go | 6 ++++-- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/server/server.go b/server/server.go index cbd70a99f..b68829c4b 100644 --- a/server/server.go +++ b/server/server.go @@ -285,7 +285,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) - m.trialVersion() + trialVersion(m.logger) // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct diff --git a/server/trial.go b/server/trial.go index 8feb6bfa4..868a177c5 100644 --- a/server/trial.go +++ b/server/trial.go @@ -1,4 +1,4 @@ -// Copyright 2017 Pilosa Corp. +// Copyright 2021 Pilosa Corp. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,54 +27,56 @@ import ( "github.com/pilosa/pilosa/v2" ) -func (m *Command) trialVersion() { +func trialVersion(logger loggerLogger) { if pilosa.TrialDeadline != "" { - endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + startTime, err := ntpServerTime(4, logger) if err != nil { - m.logger.Printf("parsing trial deadline: %v", err) + logger.Printf("reading ntp server time %v", err) os.Exit(1) } - go m.dailyCheck(endTime) + endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + if err != nil { + logger.Printf("parsing trial deadline: %v", err) + os.Exit(1) + } + maxDuration := endTime.Sub(startTime) + go expireAfter(maxDuration, logger) + go dailyCheck(maxDuration, logger) } } const trialCheckInterval = 24 * time.Hour // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(endTime time.Time) { +func dailyCheck(maxDuration time.Duration, logger loggerLogger) { + startTime := time.Now() // we get a new start time here to ensure that it has a monotonic clock + remaining := maxDuration - time.Since(startTime) + logger.Printf("Current time remaining in trial: %d days %v", remaining/(time.Hour*24), remaining%(time.Hour*24)) ticker := time.NewTicker(trialCheckInterval) - startTime, err := m.ntpServerTime(4) - if err != nil { - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) - } - runDuration := endTime.Sub(startTime) - if runDuration <= 0 { - m.logger.Printf("Trial edition of Molecula has expired, exiting now!") - os.Exit(1) - } for range ticker.C { - runningDuration := time.Since(startTime) - m.logger.Printf("Current time remaining in trial: %v", runDuration-runningDuration) - if runningDuration >= runDuration { - m.logger.Printf("Trial edition of Molecula has expired, exiting now!") - os.Exit(1) - } + remaining := maxDuration - time.Since(startTime) + logger.Printf("Current time remaining in trial: %d days %v", remaining/(time.Hour*24), remaining%(time.Hour*24)) } } -const ntpurl = "0.beevik-ntp.pool.ntp.org" +const ntpURL = "0.beevik-ntp.pool.ntp.org" const ntpRetryDelay = 100 * time.Millisecond // ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server -func (m *Command) ntpServerTime(retries int) (time.Time, error) { - t, err := ntp.Time(ntpurl) +func ntpServerTime(retries int, logger loggerLogger) (time.Time, error) { + t, err := ntp.Time(ntpURL) if err != nil && retries <= 0 { return t, err } if err != nil { time.Sleep(ntpRetryDelay) - return m.ntpServerTime(retries - 1) + return ntpServerTime(retries-1, logger) } return t, nil } + +func expireAfter(maxDuration time.Duration, logger loggerLogger) { + time.Sleep(maxDuration) + logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) +} diff --git a/version.go b/version.go index 7c3f2361b..ec27e5c6f 100644 --- a/version.go +++ b/version.go @@ -53,8 +53,10 @@ func VersionInfo() string { case buildTime != "": suffix += " (" + buildTime + ")" } - suffix += " " + GoVersion + " limited time trial ends at: " + TrialDeadline - + suffix += " " + GoVersion + if TrialDeadline != "" { + suffix += " limited time trial ends at: " + TrialDeadline + } return prefix + "Pilosa" + suffix } From d357447673b5802cab9b4feabc325edbedf8c12b Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Thu, 11 Feb 2021 10:59:32 -0600 Subject: [PATCH 09/19] change wording --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index ec27e5c6f..73a78f5c7 100644 --- a/version.go +++ b/version.go @@ -55,7 +55,7 @@ func VersionInfo() string { } suffix += " " + GoVersion if TrialDeadline != "" { - suffix += " limited time trial ends at: " + TrialDeadline + suffix += " limited time trial ends on: " + TrialDeadline } return prefix + "Pilosa" + suffix } From 1dbab1ff92b9b62c067e9182f47d660bd8b7ed13 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 12:16:08 -0600 Subject: [PATCH 10/19] add draft trial version of molecula --- Makefile | 11 ++++++++-- go.mod | 1 + go.sum | 2 ++ server/server.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 3 ++- 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 2e6cc7331..8a724c282 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice +.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice release-build-trial CLONE_URL=github.com/pilosa/pilosa MOD_VERSION=v2 @@ -11,7 +11,7 @@ BRANCH_ID := $(BRANCH)-$(GOOS)-$(GOARCH) BUILD_TIME := $(shell date -u +%FT%T%z) SHARD_WIDTH = 20 COMMIT := $(shell git describe --exact-match >/dev/null 2>&1 || git rev-parse --short HEAD) -LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT)" +LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT) -X github.com/pilosa/pilosa/v2.TrialDeadline=$(TRIAL_DEADLINE)" GO_VERSION=1.14.10 RELEASE ?= 0 RELEASE_ENABLED = $(subst 0,,$(RELEASE)) @@ -98,6 +98,13 @@ cover-viz: cover build: go build -tags='$(BUILD_TAGS)' -ldflags $(LDFLAGS) $(FLAGS) ./cmd/pilosa +# Create a single release trial build under the build directory +release-build-trial: + $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-trial-$(VERSION_ID)/pilosa" RELEASE=1 + cp NOTICE README.md build/pilosa-trial-$(VERSION_ID) + tar -cvz -C build -f build/pilosa-trial-$(VERSION_ID).tar.gz pilosa-trial-$(VERSION_ID)/ + @echo Created release trial build: build/pilosa-trial-$(VERSION_ID).tar.gz + # Create a single release build under the build directory release-build: $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-$(VERSION_ID)/pilosa" RELEASE=1 diff --git a/go.mod b/go.mod index f0d161c95..dcd6b603a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ replace github.com/hashicorp/memberlist => github.com/pilosa/memberlist v0.1.4-0 require ( github.com/CAFxX/gcnotifier v0.0.0-20190112062741-224a280d589d github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895 + github.com/beevik/ntp v0.3.0 github.com/benbjohnson/immutable v0.3.0 github.com/cespare/xxhash v1.1.0 github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect diff --git a/go.sum b/go.sum index b9d5e44d7..f1ad4ecc5 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZFFEjBj46YV4rDjvGrNxb0KMWYkL2I= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/beevik/ntp v0.3.0 h1:xzVrPrE4ziasFXgBVBZJDP0Wg/KpMwk2KHJ4Ba8GrDw= +github.com/beevik/ntp v0.3.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg= github.com/benbjohnson/immutable v0.3.0 h1:TVRhuZx2wG9SZ0LRdqlbs9S5BZ6Y24hJEHTCgWHZEIw= github.com/benbjohnson/immutable v0.3.0/go.mod h1:uc6OHo6PN2++n98KHLxW8ef4W42ylHiQSENghE1ezxI= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= diff --git a/server/server.go b/server/server.go index 530e4102a..1e9a8d053 100644 --- a/server/server.go +++ b/server/server.go @@ -39,6 +39,7 @@ import ( "golang.org/x/sync/errgroup" + "github.com/beevik/ntp" "github.com/pelletier/go-toml" "github.com/pilosa/pilosa/v2" "github.com/pilosa/pilosa/v2/boltdb" @@ -285,6 +286,16 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) + if pilosa.TrialDeadline != "" { + layout := "2006-02-02" + pilosa.TrialDeadline = "2021-02-06" + endTime, err := time.Parse(layout, pilosa.TrialDeadline) + if err != nil { + return errors.Wrap(err, "parsing curTime from make file") + } + go m.dailyCheck(endTime) + } + // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct // the environment, so that pilosa/txfactory.go can determine the @@ -478,6 +489,48 @@ func (m *Command) SetupServer() error { return errors.Wrap(err, "new handler") } +// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days +func (m *Command) dailyCheck(end time.Time) error { + ticker := time.NewTicker(5 * time.Second) + var err error + for range ticker.C { + cur, err := m.ntpServerTime() + if err != nil { + return errors.Wrap(err, "reading ntp server time") + } + + m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) + if end.Sub(cur) <= 0 { + m.logger.Printf("end: %v", end) // test line + m.logger.Printf("cur: %v", cur) // test line + m.logger.Printf("Your free trial has ended") + os.Exit(0) //is 0 the right exit number? + } + } + return errors.Wrap(err, "reading ntp server time") +} + +// ntpServerTime attempts to reach ntp servers with delays between each attempt +func (m *Command) ntpServerTime() (time.Time, error) { + curTime, err := ntp.Time("0.beevik-ntp.pool.ntp.org") + errCount := 0 + if err != nil { + for i := 0; i < 4; i++ { + curTime, err = ntp.Time("0.beevik-ntp.pool.ntp.org") + if err != nil { + errCount++ + if errCount >= 4 { + return curTime, err + } + time.Sleep(100 * time.Millisecond) + } else { + break + } + } + } + return curTime, err +} + // setupNetworking sets up internode communication based on the configuration. func (m *Command) setupNetworking() error { if m.Config.Cluster.Disabled { diff --git a/version.go b/version.go index 91a5841b7..7c3f2361b 100644 --- a/version.go +++ b/version.go @@ -25,6 +25,7 @@ var Variant string var BuildTime string var LatticeCommit string var GoVersion string = runtime.Version() +var TrialDeadline string func VersionInfo() string { var prefix string @@ -52,7 +53,7 @@ func VersionInfo() string { case buildTime != "": suffix += " (" + buildTime + ")" } - suffix += " " + GoVersion + suffix += " " + GoVersion + " limited time trial ends at: " + TrialDeadline return prefix + "Pilosa" + suffix } From 9840ae67e4fa43ce0912038a13b81c91b2fee8da Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 13:40:57 -0600 Subject: [PATCH 11/19] remove error return type --- server/server.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/server.go b/server/server.go index 1e9a8d053..7ce26a8d0 100644 --- a/server/server.go +++ b/server/server.go @@ -490,13 +490,14 @@ func (m *Command) SetupServer() error { } // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(end time.Time) error { +func (m *Command) dailyCheck(end time.Time) { ticker := time.NewTicker(5 * time.Second) var err error for range ticker.C { cur, err := m.ntpServerTime() if err != nil { - return errors.Wrap(err, "reading ntp server time") + errors.Wrap(err, "reading ntp server time") + os.Exit(1) } m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) @@ -507,7 +508,8 @@ func (m *Command) dailyCheck(end time.Time) error { os.Exit(0) //is 0 the right exit number? } } - return errors.Wrap(err, "reading ntp server time") + errors.Wrap(err, "reading ntp server time") + os.Exit(1) } // ntpServerTime attempts to reach ntp servers with delays between each attempt From d02ba3274340c07b45d3169b52a98a7fb08042c8 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 14:25:57 -0600 Subject: [PATCH 12/19] alter error output to satisfy test --- server/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/server.go b/server/server.go index 7ce26a8d0..3e416436e 100644 --- a/server/server.go +++ b/server/server.go @@ -496,7 +496,7 @@ func (m *Command) dailyCheck(end time.Time) { for range ticker.C { cur, err := m.ntpServerTime() if err != nil { - errors.Wrap(err, "reading ntp server time") + m.logger.Printf("reading ntp server time %v", err) os.Exit(1) } @@ -508,7 +508,7 @@ func (m *Command) dailyCheck(end time.Time) { os.Exit(0) //is 0 the right exit number? } } - errors.Wrap(err, "reading ntp server time") + m.logger.Printf("reading ntp server time %v", err) os.Exit(1) } From 7fd3fc775fb0d7e3d99458b67d5746d9ac1235fc Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Fri, 5 Feb 2021 15:40:55 -0600 Subject: [PATCH 13/19] remove test code --- server/server.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/server/server.go b/server/server.go index 3e416436e..089d29dba 100644 --- a/server/server.go +++ b/server/server.go @@ -287,8 +287,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) if pilosa.TrialDeadline != "" { - layout := "2006-02-02" - pilosa.TrialDeadline = "2021-02-06" + layout := "2006-01-02" endTime, err := time.Parse(layout, pilosa.TrialDeadline) if err != nil { return errors.Wrap(err, "parsing curTime from make file") @@ -499,13 +498,10 @@ func (m *Command) dailyCheck(end time.Time) { m.logger.Printf("reading ntp server time %v", err) os.Exit(1) } - m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) if end.Sub(cur) <= 0 { - m.logger.Printf("end: %v", end) // test line - m.logger.Printf("cur: %v", cur) // test line m.logger.Printf("Your free trial has ended") - os.Exit(0) //is 0 the right exit number? + os.Exit(0) } } m.logger.Printf("reading ntp server time %v", err) From c60893a8eec2bab013a33a6b33e974c8d32c2c9b Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Tue, 9 Feb 2021 15:39:32 -0600 Subject: [PATCH 14/19] edit and move trial code from server.go to trial.go and combine release build targets --- Makefile | 16 ++++--------- server/server.go | 51 +--------------------------------------- server/trial.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 server/trial.go diff --git a/Makefile b/Makefile index 8a724c282..e66f44249 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ BUILD_TIME := $(shell date -u +%FT%T%z) SHARD_WIDTH = 20 COMMIT := $(shell git describe --exact-match >/dev/null 2>&1 || git rev-parse --short HEAD) LDFLAGS="-X github.com/pilosa/pilosa/v2.Version=$(VERSION) -X github.com/pilosa/pilosa/v2.BuildTime=$(BUILD_TIME) -X github.com/pilosa/pilosa/v2.Variant=$(VARIANT) -X github.com/pilosa/pilosa/v2.Commit=$(COMMIT) -X github.com/pilosa/pilosa/v2.LatticeCommit=$(LATTICE_COMMIT) -X github.com/pilosa/pilosa/v2.TrialDeadline=$(TRIAL_DEADLINE)" +TRIAL_STRING = $(if $(TRIAL_DEADLINE),"-trial-$(TRIAL_DEADLINE)","") GO_VERSION=1.14.10 RELEASE ?= 0 RELEASE_ENABLED = $(subst 0,,$(RELEASE)) @@ -98,19 +99,12 @@ cover-viz: cover build: go build -tags='$(BUILD_TAGS)' -ldflags $(LDFLAGS) $(FLAGS) ./cmd/pilosa -# Create a single release trial build under the build directory -release-build-trial: - $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-trial-$(VERSION_ID)/pilosa" RELEASE=1 - cp NOTICE README.md build/pilosa-trial-$(VERSION_ID) - tar -cvz -C build -f build/pilosa-trial-$(VERSION_ID).tar.gz pilosa-trial-$(VERSION_ID)/ - @echo Created release trial build: build/pilosa-trial-$(VERSION_ID).tar.gz - # Create a single release build under the build directory release-build: - $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa-$(VERSION_ID)/pilosa" RELEASE=1 - cp NOTICE README.md LICENSE build/pilosa-$(VERSION_ID) - tar -cvz -C build -f build/pilosa-$(VERSION_ID).tar.gz pilosa-$(VERSION_ID)/ - @echo Created release build: build/pilosa-$(VERSION_ID).tar.gz + $(MAKE) $(if $(DOCKER_BUILD),docker-)build FLAGS="-o build/pilosa$(TRIAL_STRING)-$(VERSION_ID)/pilosa" RELEASE=1 + cp NOTICE README.md LICENSE build/pilosa$(TRIAL_STRING)-$(VERSION_ID) + tar -cvz -C build -f build/pilosa$(TRIAL_STRING)-$(VERSION_ID).tar.gz pilosa$(TRIAL_STRING)-$(VERSION_ID)/ + @echo Created release build: build/pilosa$(TRIAL_STRING)-$(VERSION_ID).tar.gz # Error out if there are untracked changes in Git check-clean: diff --git a/server/server.go b/server/server.go index 089d29dba..cbd70a99f 100644 --- a/server/server.go +++ b/server/server.go @@ -39,7 +39,6 @@ import ( "golang.org/x/sync/errgroup" - "github.com/beevik/ntp" "github.com/pelletier/go-toml" "github.com/pilosa/pilosa/v2" "github.com/pilosa/pilosa/v2/boltdb" @@ -286,14 +285,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) - if pilosa.TrialDeadline != "" { - layout := "2006-01-02" - endTime, err := time.Parse(layout, pilosa.TrialDeadline) - if err != nil { - return errors.Wrap(err, "parsing curTime from make file") - } - go m.dailyCheck(endTime) - } + m.trialVersion() // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct @@ -488,47 +480,6 @@ func (m *Command) SetupServer() error { return errors.Wrap(err, "new handler") } -// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(end time.Time) { - ticker := time.NewTicker(5 * time.Second) - var err error - for range ticker.C { - cur, err := m.ntpServerTime() - if err != nil { - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) - } - m.logger.Printf("Current time remaining in trial: %v", end.Sub(cur)) - if end.Sub(cur) <= 0 { - m.logger.Printf("Your free trial has ended") - os.Exit(0) - } - } - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) -} - -// ntpServerTime attempts to reach ntp servers with delays between each attempt -func (m *Command) ntpServerTime() (time.Time, error) { - curTime, err := ntp.Time("0.beevik-ntp.pool.ntp.org") - errCount := 0 - if err != nil { - for i := 0; i < 4; i++ { - curTime, err = ntp.Time("0.beevik-ntp.pool.ntp.org") - if err != nil { - errCount++ - if errCount >= 4 { - return curTime, err - } - time.Sleep(100 * time.Millisecond) - } else { - break - } - } - } - return curTime, err -} - // setupNetworking sets up internode communication based on the configuration. func (m *Command) setupNetworking() error { if m.Config.Cluster.Disabled { diff --git a/server/trial.go b/server/trial.go new file mode 100644 index 000000000..85df72422 --- /dev/null +++ b/server/trial.go @@ -0,0 +1,61 @@ +package server + +import ( + "os" + "time" + + "github.com/beevik/ntp" + "github.com/pilosa/pilosa/v2" +) + +func (m *Command) trialVersion() { + if pilosa.TrialDeadline != "" { + endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + if err != nil { + m.logger.Printf("parsing curTime from make file: %v", err) + os.Exit(1) + } + go m.dailyCheck(endTime) + } +} + +const hoursPerCheck = 24 * time.Hour + +// dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days +func (m *Command) dailyCheck(endTime time.Time) { + ticker := time.NewTicker(hoursPerCheck) + startTime, err := m.ntpServerTime(4) + if err != nil { + m.logger.Printf("reading ntp server time %v", err) + os.Exit(1) + } + runDuration := endTime.Sub(startTime) + if runDuration <= 0 { + m.logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) + } + for range ticker.C { + runningDuration := time.Since(startTime) + m.logger.Printf("Current time remaining in trial: %v", runDuration-runningDuration) + if runningDuration >= runDuration { + m.logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) + } + } +} + +const url = "0.beevik-ntp.pool.ntp.org" +const timeRequestDelay = 100 * time.Millisecond + +// ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server +func (m *Command) ntpServerTime(retries int) (time.Time, error) { + t, err := ntp.Time(url) + if err != nil && retries <= 0 { + return t, err + } + if err != nil { + time.Sleep(timeRequestDelay) + return m.ntpServerTime(retries - 1) + } + return t, nil +} From c973fae6aa4192e858c960b681950b275ac3055e Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Wed, 10 Feb 2021 08:21:36 -0600 Subject: [PATCH 15/19] remove remaining release-build-trial --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e66f44249..0f06e1b92 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice release-build-trial +.PHONY: build check-clean clean build-lattice cover cover-viz default docker docker-build docker-test docker-tag-push generate generate-protoc generate-pql generate-statik gometalinter install install-build-deps install-golangci-lint install-gometalinter install-protoc install-protoc-gen-gofast install-peg install-statik prerelease prerelease-upload release release-build test testv testv-race testvsub testvsub-race test-txstore-rbf lattice CLONE_URL=github.com/pilosa/pilosa MOD_VERSION=v2 From ddcf9fe1e8c360961d87cb4bfebe124105a40f8b Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Wed, 10 Feb 2021 12:37:13 -0600 Subject: [PATCH 16/19] add licence header --- server/trial.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/server/trial.go b/server/trial.go index 85df72422..8feb6bfa4 100644 --- a/server/trial.go +++ b/server/trial.go @@ -1,3 +1,22 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Package server contains the `pilosa server` subcommand which runs Pilosa +// itself. The purpose of this package is to define an easily tested Command +// object which handles interpreting configuration and setting up all the +// objects that Pilosa needs. + package server import ( @@ -12,18 +31,18 @@ func (m *Command) trialVersion() { if pilosa.TrialDeadline != "" { endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) if err != nil { - m.logger.Printf("parsing curTime from make file: %v", err) + m.logger.Printf("parsing trial deadline: %v", err) os.Exit(1) } go m.dailyCheck(endTime) } } -const hoursPerCheck = 24 * time.Hour +const trialCheckInterval = 24 * time.Hour // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days func (m *Command) dailyCheck(endTime time.Time) { - ticker := time.NewTicker(hoursPerCheck) + ticker := time.NewTicker(trialCheckInterval) startTime, err := m.ntpServerTime(4) if err != nil { m.logger.Printf("reading ntp server time %v", err) @@ -44,17 +63,17 @@ func (m *Command) dailyCheck(endTime time.Time) { } } -const url = "0.beevik-ntp.pool.ntp.org" -const timeRequestDelay = 100 * time.Millisecond +const ntpurl = "0.beevik-ntp.pool.ntp.org" +const ntpRetryDelay = 100 * time.Millisecond // ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server func (m *Command) ntpServerTime(retries int) (time.Time, error) { - t, err := ntp.Time(url) + t, err := ntp.Time(ntpurl) if err != nil && retries <= 0 { return t, err } if err != nil { - time.Sleep(timeRequestDelay) + time.Sleep(ntpRetryDelay) return m.ntpServerTime(retries - 1) } return t, nil From dd8d6ebfb197c8ec88b68101abed46bceaa8ba49 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Thu, 11 Feb 2021 10:36:04 -0600 Subject: [PATCH 17/19] Respond to code review feedback change logic in version.go so that the trial related messages only appear on trial versions of molecula convert Command methods in trial.go to functions and pass a loggerLogger variable instead since that was the only piece of Command being used add a function named expireAfter which seperately runs similar functionality to what was previously in daily check with chnages directed at stopping users from changing their internal clock date change variable names and placement to be more readable and organized --- server/server.go | 2 +- server/trial.go | 54 +++++++++++++++++++++++++----------------------- version.go | 6 ++++-- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/server/server.go b/server/server.go index cbd70a99f..b68829c4b 100644 --- a/server/server.go +++ b/server/server.go @@ -285,7 +285,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) - m.trialVersion() + trialVersion(m.logger) // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct diff --git a/server/trial.go b/server/trial.go index 8feb6bfa4..868a177c5 100644 --- a/server/trial.go +++ b/server/trial.go @@ -1,4 +1,4 @@ -// Copyright 2017 Pilosa Corp. +// Copyright 2021 Pilosa Corp. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -27,54 +27,56 @@ import ( "github.com/pilosa/pilosa/v2" ) -func (m *Command) trialVersion() { +func trialVersion(logger loggerLogger) { if pilosa.TrialDeadline != "" { - endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + startTime, err := ntpServerTime(4, logger) if err != nil { - m.logger.Printf("parsing trial deadline: %v", err) + logger.Printf("reading ntp server time %v", err) os.Exit(1) } - go m.dailyCheck(endTime) + endTime, err := time.Parse("2006-01-02", pilosa.TrialDeadline) + if err != nil { + logger.Printf("parsing trial deadline: %v", err) + os.Exit(1) + } + maxDuration := endTime.Sub(startTime) + go expireAfter(maxDuration, logger) + go dailyCheck(maxDuration, logger) } } const trialCheckInterval = 24 * time.Hour // dailyCheck runs in the background while a trial version of Molecula is being run, displaying daily reminders of the remaining days -func (m *Command) dailyCheck(endTime time.Time) { +func dailyCheck(maxDuration time.Duration, logger loggerLogger) { + startTime := time.Now() // we get a new start time here to ensure that it has a monotonic clock + remaining := maxDuration - time.Since(startTime) + logger.Printf("Current time remaining in trial: %d days %v", remaining/(time.Hour*24), remaining%(time.Hour*24)) ticker := time.NewTicker(trialCheckInterval) - startTime, err := m.ntpServerTime(4) - if err != nil { - m.logger.Printf("reading ntp server time %v", err) - os.Exit(1) - } - runDuration := endTime.Sub(startTime) - if runDuration <= 0 { - m.logger.Printf("Trial edition of Molecula has expired, exiting now!") - os.Exit(1) - } for range ticker.C { - runningDuration := time.Since(startTime) - m.logger.Printf("Current time remaining in trial: %v", runDuration-runningDuration) - if runningDuration >= runDuration { - m.logger.Printf("Trial edition of Molecula has expired, exiting now!") - os.Exit(1) - } + remaining := maxDuration - time.Since(startTime) + logger.Printf("Current time remaining in trial: %d days %v", remaining/(time.Hour*24), remaining%(time.Hour*24)) } } -const ntpurl = "0.beevik-ntp.pool.ntp.org" +const ntpURL = "0.beevik-ntp.pool.ntp.org" const ntpRetryDelay = 100 * time.Millisecond // ntpServerTime attempts to reach ntp servers with delays between each attempt, returning the time value of the first connected server -func (m *Command) ntpServerTime(retries int) (time.Time, error) { - t, err := ntp.Time(ntpurl) +func ntpServerTime(retries int, logger loggerLogger) (time.Time, error) { + t, err := ntp.Time(ntpURL) if err != nil && retries <= 0 { return t, err } if err != nil { time.Sleep(ntpRetryDelay) - return m.ntpServerTime(retries - 1) + return ntpServerTime(retries-1, logger) } return t, nil } + +func expireAfter(maxDuration time.Duration, logger loggerLogger) { + time.Sleep(maxDuration) + logger.Printf("Trial edition of Molecula has expired, exiting now!") + os.Exit(1) +} diff --git a/version.go b/version.go index 7c3f2361b..ec27e5c6f 100644 --- a/version.go +++ b/version.go @@ -53,8 +53,10 @@ func VersionInfo() string { case buildTime != "": suffix += " (" + buildTime + ")" } - suffix += " " + GoVersion + " limited time trial ends at: " + TrialDeadline - + suffix += " " + GoVersion + if TrialDeadline != "" { + suffix += " limited time trial ends at: " + TrialDeadline + } return prefix + "Pilosa" + suffix } From 136a4b8068f56d1d9759fad8057ee71fe426276d Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Thu, 11 Feb 2021 10:59:32 -0600 Subject: [PATCH 18/19] change wording --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index ec27e5c6f..73a78f5c7 100644 --- a/version.go +++ b/version.go @@ -55,7 +55,7 @@ func VersionInfo() string { } suffix += " " + GoVersion if TrialDeadline != "" { - suffix += " limited time trial ends at: " + TrialDeadline + suffix += " limited time trial ends on: " + TrialDeadline } return prefix + "Pilosa" + suffix } From b89dae01cc130a7734060074e53e70582d36099b Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Thu, 11 Feb 2021 16:13:04 -0600 Subject: [PATCH 19/19] clarify purpose of trialVersion by changing the name to handleTrialDeadline and adding a doc string --- lattice | 2 +- server/server.go | 2 +- server/trial.go | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lattice b/lattice index fa773628a..28c2313ec 160000 --- a/lattice +++ b/lattice @@ -1 +1 @@ -Subproject commit fa773628a276e2590785a87fbc236c7e88ea6284 +Subproject commit 28c2313ecfcd7e083d42d4e409483e968b4c421b diff --git a/server/server.go b/server/server.go index b68829c4b..b5b43cbc5 100644 --- a/server/server.go +++ b/server/server.go @@ -285,7 +285,7 @@ func (m *Command) SetupServer() error { m.logger.Printf("%s", pilosa.VersionInfo()) - trialVersion(m.logger) + handleTrialDeadline(m.logger) // If the pilosa command line uses -tx to override the // PILOSA_TXSRC env variable, then we must also correct diff --git a/server/trial.go b/server/trial.go index 868a177c5..342d979f9 100644 --- a/server/trial.go +++ b/server/trial.go @@ -27,7 +27,11 @@ import ( "github.com/pilosa/pilosa/v2" ) -func trialVersion(logger loggerLogger) { +// handleTrialDeadline checks to see if this is a trial version of Molecula that expires at some point. +// If it is, we contact an NTP server to get the current time and compare that to the trial deadline. +// We launch two goroutines, one which reminds via a log message how much time is left in the trial, +// and one which causes the process to exit once the trial is over. +func handleTrialDeadline(logger loggerLogger) { if pilosa.TrialDeadline != "" { startTime, err := ntpServerTime(4, logger) if err != nil {