Skip to content

Commit

Permalink
Make memdb txn read only and defer abort (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekStrickland authored Jan 28, 2021
1 parent 95a3c4d commit 2d4a149
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
22 changes: 13 additions & 9 deletions data/in_memory_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
// uisng go-membdb instead of postgres.
type InMemoryRepository struct {
db *memdb.MemDB
config *config.Config
}

// NewInMemoryDB is the InMemoryRepository factory method. It fulfills the same
Expand All @@ -44,40 +45,42 @@ func NewInMemoryDB(config *config.Config) (Repository, error) {
return &InMemoryRepository{}, err
}

repository := &InMemoryRepository{db}
repository := &InMemoryRepository{db, config}

config.Logger.Debug("Loading Ingredients")
repository.config.Logger.Debug("Loading Ingredients")
err = repository.loadIngredients()
if err != nil {
config.Logger.Debug(fmt.Sprintf("Failed to load ingredients with err %+v", err))
repository.config.Logger.Debug(fmt.Sprintf("Failed to load ingredients with err %+v", err))
return &InMemoryRepository{}, err
}

config.Logger.Debug("Loading coffees")
repository.config.Logger.Debug("Loading coffees")
err = repository.loadCoffees()
if err != nil {
config.Logger.Debug(fmt.Sprintf("Failed to load coffees with err %+v", err))
repository.config.Logger.Debug(fmt.Sprintf("Failed to load coffees with err %+v", err))
return &InMemoryRepository{}, err
}

config.Logger.Debug("Loading coffee ingredients")
repository.config.Logger.Debug("Loading coffee ingredients")
err = repository.loadCoffeeIngredients()
if err != nil {
config.Logger.Debug(fmt.Sprintf("Failed to load coffee ingredients with err %+v", err))
repository.config.Logger.Debug(fmt.Sprintf("Failed to load coffee ingredients with err %+v", err))
return &InMemoryRepository{}, err
}

config.Logger.Debug("Data loaded")
repository.config.Logger.Debug("Data loaded")
return repository, nil
}

// Find returns all coffees from the database
// Used to accept ctx opentracing.SpanContext
func (r *InMemoryRepository) Find() (entities.Coffees, error) {
txn := r.db.Txn(true)
txn := r.db.Txn(false)
defer txn.Abort()

iter, err := txn.Get(Coffee.String(), "id")
if err != nil {
r.config.Logger.Error("coffee-service.data.InMemoryRepository.Find failed to load coffees", err)
return nil, err
}

Expand All @@ -92,6 +95,7 @@ func (r *InMemoryRepository) Find() (entities.Coffees, error) {

innerIter, err := txn.Get(CoffeeIngredient.String(), "id")
if err != nil {
r.config.Logger.Error("coffee-service.data.InMemoryRepository.Find failed to load ingredients", err)
return nil, err
}

Expand Down
14 changes: 14 additions & 0 deletions debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Assumes kind installed locally
kind delete cluster --name coffee-service-debug

make build_linux

docker build -t coffee-service:debug .

kind create cluster --name coffee-service-debug

kind load docker-image coffee-service:debug --name coffee-service-debug

kubectl apply -f ./deployments/debug/coffee-service.yaml

kubectl get pods
68 changes: 68 additions & 0 deletions deployments/debug/coffee-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
apiVersion: v1
kind: Service
metadata:
name: coffee-service
spec:
selector:
app: coffee-service
ports:
- name: http
protocol: TCP
port: 9090
targetPort: 9090
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: coffee-service
automountServiceAccountToken: true
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coffee-service
labels:
app: coffee-service
version: v3
spec:
replicas: 1
selector:
matchLabels:
app: coffee-service
template:
metadata:
labels:
app: coffee-service
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9102"
consul.hashicorp.com/connect-inject: "true"
consul.hashicorp.com/service-meta-version: "v1"
consul.hashicorp.com/service-tags: "api"
spec:
serviceAccountName: coffee-service
containers:
- name: coffee-service
image: coffee-service:debug
ports:
- containerPort: 9090
- containerPort: 9102
env:
- name: "LOG_FORMAT"
value: "text"
- name: "LOG_LEVEL"
value: "DEBUG"
- name: "BIND_ADDRESS"
value: "localhost:9090"
- name: "METRICS_ADDRESS"
value: "localhost:9102"
- name: "VERSION"
value: "v3"
livenessProbe:
httpGet:
path: /health
port: 9090
initialDelaySeconds: 15
timeoutSeconds: 1
periodSeconds: 10
failureThreshold: 30
4 changes: 2 additions & 2 deletions deployments/v3/coffee-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ spec:
- name: "LOG_LEVEL"
value: "INFO"
- name: "BIND_ADDRESS"
value: "127.0.0.1:9090"
value: "localhost:9090"
- name: "METRICS_ADDRESS"
value: "127.0.0.1:9102"
value: "localhost:9102"
- name: "VERSION"
value: "v3"
livenessProbe:
Expand Down

0 comments on commit 2d4a149

Please sign in to comment.