This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cassandra database storage back end
- Loading branch information
Michel Zimmer
committed
Jun 22, 2022
1 parent
710750c
commit 5e46b8a
Showing
28 changed files
with
1,254 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
# bandwhichd-server depends on cassandra. | ||
# docker-compose does not detect if cassandra is up properly. | ||
# Additionally, the keyspace needs to be created manually for | ||
# now, because it is not part of the automated migrations. | ||
# | ||
# 1. Start cassandra | ||
# docker-compose up --detach cassandra | ||
# 2. Create keyspace (it takes a while until cassandra is ready and accepts connections) | ||
# docker-compose exec -- cassandra cqlsh --execute="create keyspace if not exists bandwhichd with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};" | ||
# 3. Start bandwhichd-server | ||
# docker-compose up --build --detach bandwhichd-server | ||
# 4. Follow logs | ||
# docker-compose logs --follow | ||
|
||
services: | ||
main: | ||
cassandra: | ||
image: cassandra:4.0.4 | ||
ports: | ||
- 9042:9042 | ||
bandwhichd-server: | ||
build: | ||
context: . | ||
command: | ||
- -Xmx1g | ||
- -jar | ||
- /opt/bandwhichd-server.jar | ||
environment: | ||
CONTACT_POINTS: "cassandra:9042" | ||
ports: | ||
- 8080:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/main/scala/de/neuland/bandwhichd/server/adapter/in/scheduler/MemoryScheduler.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
src/main/scala/de/neuland/bandwhichd/server/adapter/out/CappedStorage.scala
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
src/main/scala/de/neuland/bandwhichd/server/adapter/out/CassandraMigration.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package de.neuland.bandwhichd.server.adapter.out | ||
|
||
import cats.implicits.* | ||
import cats.effect.Async | ||
import com.datastax.oss.driver.api.core.cql.{SimpleStatement, Statement} | ||
import de.neuland.bandwhichd.server.boot.Configuration | ||
import de.neuland.bandwhichd.server.lib.cassandra.CassandraContext | ||
|
||
class CassandraMigration[F[_]: Async]( | ||
private val cassandraContext: CassandraContext[F] | ||
) { | ||
def migrate(configuration: Configuration): F[Unit] = | ||
for { | ||
_ <- createCidrType(configuration) | ||
_ <- createMeasurementNetworkConfigurationInterfaceType(configuration) | ||
_ <- createMeasurementNetworkConfigurationOpenSocketType(configuration) | ||
_ <- createMeasurementNetworkUtilizationConnectionType(configuration) | ||
_ <- createMeasurementsTable(configuration) | ||
} yield () | ||
|
||
private def createMeasurementsTable( | ||
configuration: Configuration | ||
): F[Unit] = | ||
cassandraContext.executeRawExpectNoRow( | ||
SimpleStatement | ||
.builder( | ||
"""create table if not exists measurements ( | ||
| agent_id uuid, | ||
| timestamp timestamp, | ||
| end_timestamp timestamp, | ||
| measurement_type ascii, | ||
| network_configuration_machine_id uuid, | ||
| network_configuration_hostname text, | ||
| network_configuration_interfaces frozen<list<frozen<measurement_network_configuration_interface>>>, | ||
| network_configuration_open_sockets frozen<list<frozen<measurement_network_configuration_open_socket>>>, | ||
| network_utilization_connections frozen<list<frozen<measurement_network_utilization_connection>>>, | ||
| primary key ((agent_id), timestamp, measurement_type), | ||
|) with clustering order by (timestamp asc)""".stripMargin | ||
) | ||
.setKeyspace(configuration.measurementsKeyspace) | ||
.build() | ||
) | ||
|
||
private def createMeasurementNetworkConfigurationInterfaceType( | ||
configuration: Configuration | ||
): F[Unit] = | ||
cassandraContext.executeRawExpectNoRow( | ||
SimpleStatement | ||
.builder( | ||
"""create type if not exists measurement_network_configuration_interface ( | ||
| name text, | ||
| is_up boolean, | ||
| networks frozen<list<frozen<cidr>>>, | ||
|)""".stripMargin | ||
) | ||
.setKeyspace(configuration.measurementsKeyspace) | ||
.build() | ||
) | ||
|
||
private def createMeasurementNetworkConfigurationOpenSocketType( | ||
configuration: Configuration | ||
): F[Unit] = | ||
cassandraContext.executeRawExpectNoRow( | ||
SimpleStatement | ||
.builder( | ||
"""create type if not exists measurement_network_configuration_open_socket ( | ||
| socket text, | ||
| protocol ascii, | ||
| maybe_process_name text, | ||
|)""".stripMargin | ||
) | ||
.setKeyspace(configuration.measurementsKeyspace) | ||
.build() | ||
) | ||
|
||
private def createMeasurementNetworkUtilizationConnectionType( | ||
configuration: Configuration | ||
): F[Unit] = | ||
cassandraContext.executeRawExpectNoRow( | ||
SimpleStatement | ||
.builder( | ||
"""create type if not exists measurement_network_utilization_connection ( | ||
| interface_name text, | ||
| local_socket text, | ||
| remote_socket text, | ||
| protocol ascii, | ||
| received bigint, | ||
| sent bigint, | ||
|)""".stripMargin | ||
) | ||
.setKeyspace(configuration.measurementsKeyspace) | ||
.build() | ||
) | ||
|
||
private def createCidrType( | ||
configuration: Configuration | ||
): F[Unit] = | ||
cassandraContext.executeRawExpectNoRow( | ||
SimpleStatement | ||
.builder( | ||
"""create type if not exists cidr ( | ||
| address inet, | ||
| prefix_bits smallint, | ||
|)""".stripMargin | ||
) | ||
.setKeyspace(configuration.measurementsKeyspace) | ||
.build() | ||
) | ||
} |
Oops, something went wrong.