Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile in geoip2 database and avoid external file reading #281

Merged
merged 1 commit into from
Oct 16, 2018

Conversation

tadovas
Copy link
Contributor

@tadovas tadovas commented Jul 4, 2018

Idea is to reduce external file dependencies and to build-in as much data/info as possible. First candidate geoip database.
Short summary:

  1. Geoip database is converted to few source files by generator.go
  2. New option for builtin country resolving for both client/server - used by default
  3. No need to package db for runtime anymore

@tadovas tadovas requested review from shroomist, donce, Waldz and zolia July 4, 2018 15:24
@Waldz
Copy link
Member

Waldz commented Jul 5, 2018

+100 kudos, awesome idea

@Waldz
Copy link
Member

Waldz commented Jul 5, 2018

@tadovas tadovas force-pushed the improvement/build-in-geoip-db branch 6 times, most recently from 68d1164 to 6cf482b Compare July 13, 2018 06:07
@tadovas tadovas changed the title WIP: Compile in geoip2 database and avoid external file reading Compile in geoip2 database and avoid external file reading Jul 13, 2018
@tadovas
Copy link
Contributor Author

tadovas commented Jul 13, 2018

@zolia @Waldz this one is ready for review

@Waldz Waldz changed the title Compile in geoip2 database and avoid external file reading WIP Compile in geoip2 database and avoid external file reading Aug 15, 2018
Copy link
Contributor

@donce donce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is shown for me on each pull request reminder, so I'm putting request changes to hide it.

zolia
zolia previously approved these changes Aug 31, 2018
Copy link
Contributor

@zolia zolia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just a note that we will need to remove 'bin/common_package/GeoLite2-Country.mmdb' and all related packaging references. Maybe in separate task.

@stale
Copy link

stale bot commented Oct 12, 2018

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 12, 2018
@tadovas tadovas changed the title WIP Compile in geoip2 database and avoid external file reading Compile in geoip2 database and avoid external file reading Oct 15, 2018
@stale stale bot removed the stale label Oct 15, 2018
@tadovas
Copy link
Contributor Author

tadovas commented Oct 15, 2018

It comes a bit important now

@tadovas tadovas force-pushed the improvement/build-in-geoip-db branch 5 times, most recently from eb26da5 to 4d9a653 Compare October 15, 2018 12:54
@tadovas
Copy link
Contributor Author

tadovas commented Oct 15, 2018

@mysteriumnetwork/core you can all take a look, while its being verified by CI

@tadovas
Copy link
Contributor Author

tadovas commented Oct 15, 2018

@Waldz @vkuznecovas ping. Ready for review

@@ -28,8 +28,12 @@ var (
Usage: "Address (URL form) of ipify service",
Value: "https://api.ipify.org/",
}
locationBuiltInDbFlag = cli.BoolTFlag{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont need this flag, compiled-in should be default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

cmd/di.go Outdated
di.LocationResolver = location.NewResolverFake(options.Country)
di.LocationResolver = location.NewStaticResolver(options.Country)
case options.BuiltInDb:
di.LocationResolver = location.NewBuiltInResolver()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewBuiltInResolver should be default resolver if no other option other resolver given

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return "", errors.New("failed to parse IP")
}

countryRecord, err := r.db.Country(ipObject)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like duplicated logic, lets DRY with composition:

DBResolver{
  db resource
}

BuiltinDBResolver {
    Resolver DBResolver
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DRYied

Country string
IpifyUrl string
ExternalDb string
BuiltInDb bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If build-in is default, we dont need it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Removed

vkuznecovas
vkuznecovas previously approved these changes Oct 15, 2018

// NewBuiltInResolver returns new BuiltInDbResolver
func NewBuiltInResolver() Resolver {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the empty line at the start of the func seems odd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


// ResolveCountry maps given ip to country
func (r *BuiltInDbResolver) ResolveCountry(ip string) (string, error) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the empty line at the start of the func seems odd.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


country := countryRecord.Country.IsoCode
if country == "" {
country = countryRecord.RegisteredCountry.IsoCode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if country = countryRecord.RegisteredCountry.IsoCode; country == "" {
return "", errors.New("failed to resolve country")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I am not sure if this looks more readable :)

@tadovas tadovas force-pushed the improvement/build-in-geoip-db branch 2 times, most recently from 2b7555b to e4aed78 Compare October 15, 2018 15:39
@tadovas
Copy link
Contributor Author

tadovas commented Oct 15, 2018

@Waldz @vkuznecovas all comments addressed. Ready for re-review

@tadovas tadovas force-pushed the improvement/build-in-geoip-db branch from e4aed78 to dde38ea Compare October 15, 2018 15:49
Copy link
Member

@soffokl soffokl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, did we try to use existing libraries that allows embedding files into executable binary?

cmd/di.go Outdated
@@ -43,6 +41,7 @@ import (
"github.com/mysteriumnetwork/node/services/openvpn"
openvpn_service "github.com/mysteriumnetwork/node/services/openvpn/service"
"github.com/mysteriumnetwork/node/session"
"path/filepath"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look goimports formatted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bin/check_goimports: line 23: goimports: command not found
hm, looks like we have broken goimports check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue for fixing goimports on the CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed code.


// EncodedDataLoader returns emmbeded database as byte array
func EncodedDataLoader(data string, originalSize int, compressed bool) (decompressed []byte, err error) {
var reader io.Reader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be just reader := base64.NewDecoder(base64.RawStdEncoding, strings.NewReader(data))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot. Because of the compressed flag, reader can be further decorated with gzip.Reader

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Got your point. It can be just reader := ...
Thanks.

@tadovas
Copy link
Contributor Author

tadovas commented Oct 16, 2018

Yea I checked a few embedding libraries, didn't find small and efficient enough, they all were either to big, ugly to use, or not compatible with go generate style integration

@tadovas tadovas force-pushed the improvement/build-in-geoip-db branch from dde38ea to 3206da2 Compare October 16, 2018 06:01
vkuznecovas
vkuznecovas previously approved these changes Oct 16, 2018
soffokl
soffokl previously approved these changes Oct 16, 2018
Copy link
Member

@soffokl soffokl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

const originalSize = 3303090
const dbData = "" + dbDataPart0 + dbDataPart1

// LoadData returns emmbeded database as byte array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: emmbeded -> embedded

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -29,9 +29,9 @@ var (
Value: "https://api.ipify.org/",
}
locationDatabaseFlag = cli.StringFlag{
Name: "location.database",
Name: "location.db.external",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not change contract

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

location.database

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@tadovas tadovas dismissed stale reviews from soffokl and vkuznecovas via 537a4e3 October 16, 2018 10:55
@tadovas tadovas force-pushed the improvement/build-in-geoip-db branch from 3206da2 to 537a4e3 Compare October 16, 2018 10:55
@tadovas
Copy link
Contributor Author

tadovas commented Oct 16, 2018

@soffokl @vkuznecovas reapprove please

@tadovas tadovas merged commit c61e995 into master Oct 16, 2018
@tadovas tadovas deleted the improvement/build-in-geoip-db branch October 16, 2018 13:07
@Waldz Waldz mentioned this pull request Oct 24, 2018
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants