Skip to content

mikelue/valor-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Valor code

Valor code is a simulator for back-end technologies.

The technologies is going to be used:

  1. Spring framework: SpringBoot, SpringData, WebFlux

  2. Docker for deployment

  3. Kubernetes with kustomized. Configurations for minkube

  4. kafka for queuing/event sourcing

  5. PostgreSQL and Cassandra

Of course, Java

Settings

Settings can be put in java command optional -D<prop>=<value>

Required cleaning

  • spring.datasource.url - For database URL of PostgreSQL

  • spring.datasource.username - For connecting to PostgreSQL

  • spring.datasource.password - For connecting to PostgreSQL

  • spring.data.cassandra.keyspace-name - The keyspace on which this application is using

  • spring.data.cassandra.contact-points - For connecting to Cassandra

  • spring.data.cassandra.local-datacenter - For name of data center

  • spring.kafka.bootstrap-servers - For connecting to Kafka

  • spring.kafka.consumer.group-id - The group id of consumer

Optional properties

  • valor.farming.duration.too-long-scheduled-activities(default: PT5M) - The Duration string(e.g. PT5M) for some of activities are kept same status since update time.

Deployment

Run by Maven

cd farming

mvn -q spring-boot:run

Build application(Docker)

cd farming

mvn -DskipTests=true package
docker build --build-arg JAR_FILE=target/*.jar -t valor-code/farming .

Setting up cluster of Kubernetes

In ./kustomize directory, you can use kustomize to intialize cluster in Minikube.

kubectl kustomize ./kustomize | kubectl apply -l -f -

The cluster contains nodes by default:

  • Two nodes of Kafka

    • Depends on one node of Zookeeper

  • Two nodes of Cassandra

Don’t forget to set up PostgreSQL by your favor environment.

Migration for database schemas

PostgreSQL

By Maven plugin of Liquibase.

cd farming
mvn liquibase:update -Ddb.url= -Ddb.username= -Ddb.password=

Required properties:

  • db.url - As the URL of PostgreSQL

  • db.username - For connecting to PostgreSQL

  • db.password - For connecting to PostgreSQL

Cassandra

By cassandra-migrate(Built by Python language)

cd farming/cassandra
cassandra-migrate -H cassandra-0 migrate

Scripts

In ./scripts directory, there are some scripts to perform testing.

  • new-lands.sh - Creates 10 lands

  • ask-sowing.sh - Ask random number of blocks for sowing; every land will get asked.

  • ask-cleaning.sh - Ask random number of blocks for cleaning; every land will get asked.

Domain objects

Land

A land is place with following attributes:

  • A land has an unique name over universe.

  • The number of squares this land holds.

  • The climates: tropical, dry, mild, continental, polar

Block

A land contains 1:N blocks for cultivating crops.

Block status:

  1. A scheduled(sow, harvest, or clean) block cannot be change anymore until the process handle it.

  2. A empty block can be scheduled for sowing

  3. A occupied and mature block can be scheduled for harvesting

  4. A occupied block can be scheduled for cleaning

Crops

A few crops can be grown on land:

  • manioc, rice, yams - can be grown in tropical, mild, or continental land.

  • grape, tomatoe, pumpkin - can be grown in mild, continental, dry land.

  • kale, spinach, lettuce - can be grown in mild, continental, polar land.

Table 1. Growing time for crops
Crop Sowing time(seconds) Growing time(seconds) Harvest time(seconds) Harvest quantity

manioc

2 ~ 5

15 ~ 20

2 ~ 5

5 ~ 10

rice

2 ~ 5

17 ~ 23

2 ~ 5

10 ~ 20

yams

2 ~ 5

15 ~ 25

2 ~ 5

10 ~ 15

grape

3 ~ 8

10 ~ 20

3 ~ 7

5 ~ 15

tomato

3 ~ 8

10 ~ 20

3 ~ 7

15 ~ 30

pumpkin

3 ~ 8

10 ~ 15

3 ~ 7

3 ~ 8

kale

4 ~ 10

10 ~ 20

2 ~ 5

5 ~ 12

spinach

4 ~ 10

10 ~ 25

5 ~ 10

10 ~ 30

lettuce

4 ~ 10

5 ~ 12

5 ~ 10

10 ~ 20

APIs

Internal works

  • Data Schema

  • Land and blocks are created by API.

  • Sowing API would scheduled available blocks on a land for target crop.

  • Harvesting service would search matured crops and scheduled it for harvesting.

  • Cron service would search scheduled blocks that their 'update_time' is old than certain duration.

    • See property value of valor.farming.duration.too-long-scheduled-activities

Local developing

Maven profiles

  • DEFAULT(unit test)

    • db.database - valor_farming_ut

    • db.cassandra.keyspace - valor_farming_ut

    Verify by single IT class
    mvn -P it-dev -q verify -Dit.test=BlockControllerIT
  • it-dev - Skips *Test.java, used with Maven verify.

  • it-database - Different PostgreSQL database and Cassandra keyspace .Used by full-started application

    • db.database - valor_farming

    • db.cassandra.keyspace - valor_farming

    Used with full-started application
    mvn -P it-database spring-boot:start
    
    mvn -P it-database spring-boot:run

Use Kafka/Cassandra cluster with minicube

Listening properties:

  • Kafka - Use advertised.listeners to advertise client the real connection for every StatefulSet pod.

  • Cassandra - Use broadcast_rpc_address to advertise client the real connection for every StatefulSet pod.

Setting-up

  1. Assign multiple IP addresses on host of minicube. Setting up Multiple IPs on Ubuntu

  2. Adds entries to /etc/hosts:

    /etc/hosts
    <ip-1> kafka-0.kafka.default.svc.cluster.local kafka-0
    <ip-2> kafka-1.kafka.default.svc.cluster.local kafka-1
    <ip-3> kafka-2.kafka.default.svc.cluster.local kafka-2
    
    <ip-1> cassandra-0.cassandra.default.svc.cluster.local cassandra-0
    <ip-2> cassandra-1.cassandra.default.svc.cluster.local cassandra-1
    <ip-3> cassandra-2.cassandra.default.svc.cluster.local cassandra-2
  3. Start port-forward

    kubectl port-forward --address=<ip-0> po/kafka-0 9092:kafka &
    kubectl port-forward --address=<ip-1> po/kafka-1 9092:kafka &
    kubectl port-forward --address=<ip-2> po/kafka-2 9092:kafka &
    
    kubectl port-forward --address=<ip-0> po/cassandra-0 9092:cassandra &
    kubectl port-forward --address=<ip-1> po/cassandra-1 9092:cassandra &
    kubectl port-forward --address=<ip-2> po/cassandra-2 9092:cassandra &

With above tech, you can use kafka-0, kafak-1, cassandra-0, etc., to bootstrap client of Kafka.

Example
./kafka-topics.sh --bootstrap-server kafka-0:9092 --create --topic sample-topic-1

If you are using WSL, you may like to modify the file of %WINDIR%\System32\drivers\etc\hosts.

About

Some playful, imaginary application, just for self-training and for fun.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published