Skip to content

Commit

Permalink
Change index setting to contacts_index and rename some stuff for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Oct 11, 2022
1 parent ca472f4 commit 2970fd3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/rp-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
}

idxrs := []indexers.Indexer{
indexers.NewContactIndexer(cfg.ElasticURL, cfg.Index, 500),
indexers.NewContactIndexer(cfg.ElasticURL, cfg.ContactsIndex, 500),
}

if cfg.Rebuild {
Expand Down
39 changes: 20 additions & 19 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@ package indexer
import "os"

type Config struct {
ElasticURL string `help:"the url for our elastic search instance"`
DB string `help:"the connection string for our database"`
Index string `help:"the alias for our contact index"`
Poll int `help:"the number of seconds to wait between checking for updated contacts"`
Rebuild bool `help:"whether to rebuild the index, swapping it when complete, then exiting (default false)"`
Cleanup bool `help:"whether to remove old indexes after a rebuild"`
LogLevel string `help:"the log level, one of error, warn, info, debug"`
SentryDSN string `help:"the sentry configuration to log errors to, if any"`
ContactsShards int `help:"The number of shards to use for the contacts index"`
ContactsReplicas int `help:"The number of replicas to use for the contacts index"`

ElasticURL string `help:"the url for our elastic search instance"`
DB string `help:"the connection string for our database"`
Poll int `help:"the number of seconds to wait between checking for database updates"`
Rebuild bool `help:"whether to rebuild the index, swapping it when complete, then exiting (default false)"`
Cleanup bool `help:"whether to remove old indexes after a rebuild"`
LogLevel string `help:"the log level, one of error, warn, info, debug"`
SentryDSN string `help:"the sentry configuration to log errors to, if any"`
LibratoUsername string `help:"the username that will be used to authenticate to Librato"`
LibratoToken string `help:"the token that will be used to authenticate to Librato"`
InstanceName string `help:"the unique name of this instance used for analytics"`

ContactsIndex string `help:"the alias to use for the contact index"`
ContactsShards int `help:"the number of shards to use for the contacts index"`
ContactsReplicas int `help:"the number of replicas to use for the contacts index"`
}

func NewDefaultConfig() *Config {
hostname, _ := os.Hostname()

return &Config{
ElasticURL: "http://localhost:9200",
DB: "postgres://localhost/temba?sslmode=disable",
Index: "contacts",
Poll: 5,
Rebuild: false,
Cleanup: false,
LogLevel: "info",
InstanceName: hostname,
ElasticURL: "http://localhost:9200",
DB: "postgres://localhost/temba?sslmode=disable",
Poll: 5,
Rebuild: false,
Cleanup: false,
LogLevel: "info",
InstanceName: hostname,

ContactsIndex: "contacts",
ContactsShards: 2,
ContactsReplicas: 1,
}
Expand Down
12 changes: 5 additions & 7 deletions indexers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Indexer interface {
Stats() Stats
}

type ElasticSettings struct {
type IndexDefinition struct {
Settings struct {
Index struct {
NumberOfShards int `json:"number_of_shards"`
Expand Down Expand Up @@ -111,7 +111,7 @@ func (i *baseIndexer) FindIndexes() []string {
// that index to `contacts`.
//
// If the day-specific name already exists, we append a .1 or .2 to the name.
func (i *baseIndexer) createNewIndex(indexSettings ElasticSettings) (string, error) {
func (i *baseIndexer) createNewIndex(def *IndexDefinition) (string, error) {
// create our day-specific name
index := fmt.Sprintf("%s_%s", i.name, time.Now().Format("2006_01_02"))
idx := 0
Expand All @@ -133,11 +133,9 @@ func (i *baseIndexer) createNewIndex(indexSettings ElasticSettings) (string, err
}

// create the new index
settings, err := json.Marshal(indexSettings)
if err != nil {
return "", err
}
_, err = utils.MakeJSONRequest(http.MethodPut, fmt.Sprintf("%s/%s?include_type_name=true", i.elasticURL, index), settings, nil)
settings := jsonx.MustMarshal(def)

_, err := utils.MakeJSONRequest(http.MethodPut, fmt.Sprintf("%s/%s?include_type_name=true", i.elasticURL, index), settings, nil)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions indexers/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package indexers_test
import (
"context"
"database/sql"
"io/ioutil"
"log"
"os"
"sort"
Expand All @@ -22,7 +21,7 @@ const elasticURL = "http://localhost:9200"
const aliasName = "indexer_test"

func setup(t *testing.T) (*sql.DB, *elastic.Client) {
testDB, err := ioutil.ReadFile("../testdb.sql")
testDB, err := os.ReadFile("../testdb.sql")
require.NoError(t, err)

db, err := sql.Open("postgres", "postgres://nyaruka:nyaruka@localhost:5432/elastic_test?sslmode=disable")
Expand Down
27 changes: 13 additions & 14 deletions indexers/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"bytes"
"database/sql"
_ "embed"
"encoding/json"
"fmt"
"time"

"github.com/nyaruka/gocommon/jsonx"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"time"
)

//go:embed contacts.settings.json
var contactsSettingsFile []byte

var contactsSettings ElasticSettings
//go:embed contacts.index.json
var contactsIndexDefinition []byte

// ContactIndexer is an indexer for contacts
type ContactIndexer struct {
Expand All @@ -32,7 +31,7 @@ func NewContactIndexer(elasticURL, name string, batchSize int) *ContactIndexer {
}

// Index indexes modified contacts and returns the name of the concrete index
func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool, shards int, replicas int) (string, error) {
func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool, shards, replicas int) (string, error) {
var err error

// find our physical index
Expand All @@ -48,13 +47,13 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool, shards int, re

// doesn't exist or we are rebuilding, create it
if physicalIndex == "" || rebuild {
err = json.Unmarshal(contactsSettingsFile, &contactsSettings)
if err != nil {
return "", errors.Wrap(err, "error unmarshalling embeded contacts.settings.json file")
}
contactsSettings.Settings.Index.NumberOfShards = shards
contactsSettings.Settings.Index.NumberOfReplicas = replicas
physicalIndex, err = i.createNewIndex(contactsSettings)
def := &IndexDefinition{}
jsonx.MustUnmarshal(contactsIndexDefinition, def)

def.Settings.Index.NumberOfShards = shards
def.Settings.Index.NumberOfReplicas = replicas

physicalIndex, err = i.createNewIndex(def)
if err != nil {
return "", errors.Wrap(err, "error creating new index")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1,
"number_of_shards": -1,
"number_of_replicas": -1,
"routing_partition_size": 1
},
"analysis": {
Expand Down

0 comments on commit 2970fd3

Please sign in to comment.