Skip to content

Commit

Permalink
[core] add protobuf serializer
Browse files Browse the repository at this point in the history
Add a protobuf serializer that is more space and cpu efficient.
  • Loading branch information
dmichel1 committed Nov 25, 2018
1 parent c164ffd commit d9459c4
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>

<dependency>
<groupId>eu.toolchain.async</groupId>
<artifactId>tiny-async-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class BuiltInSerializers implements FastForwardModule {
@Override
public void setup() throws Exception {
context.registerSerializer("spotify100", Spotify100Serializer.class);
context.registerSerializer("spotify100proto", Spotify100ProtoSerializer.class);
context.registerSerializer("to-string", ToStringSerializer.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
* -\-\-
* FastForward Core
* --
* Copyright (C) 2016 - 2018 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.ffwd.serializer;

import com.spotify.ffwd.model.Event;
import com.spotify.ffwd.model.Metric;
import com.spotify.proto.Spotify100;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Spotify100ProtoSerializer implements Serializer {

@Override
public byte[] serialize(final Event event) throws Exception {
log.debug("Serializing events is not supported");
return new byte[0];
}

@Override
public byte[] serialize(final Metric metric) throws Exception {
return Spotify100.Metric.newBuilder()
.setKey(metric.getKey())
.setTime(metric.getTime().getTime())
.setValue(metric.getValue())
.putAllTags(metric.getTags())
.putAllResource(metric.getResource())
.build()
.toByteArray();
}
}
27 changes: 27 additions & 0 deletions core/src/main/proto/spotify_100.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";

package spotify.metric;
option java_package = "com.spotify.proto";

option optimize_for = SPEED;

message Metric {
// key of metric.
string key = 1;

// time in ms when metric was generated.
int64 time = 2;

// value of metric.
double value = 3;

// tags associated to a metric, used to be referred to as attributes.
map<string, string> tags = 4;

// resource "tags" associated to metric.
map<string, string> resource = 5;
}

message Message {
Metric metric = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*-
* -\-\-
* FastForward Core
* --
* Copyright (C) 2016 - 2018 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.ffwd.serializer;

import static org.hamcrest.CoreMatchers.any;
import static org.junit.Assert.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.spotify.ffwd.model.Metric;
import java.time.Instant;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;

public class TestSpotify100ProtoSerializer {
private Spotify100ProtoSerializer spotify100ProtoSerializer;
private Metric metric;

@Before
public void setup() {
spotify100ProtoSerializer = new Spotify100ProtoSerializer();
}

@Test
public void testSerializeMetric() throws Exception {
metric = new Metric("",
0.0,
Date.from(Instant.ofEpochSecond(1542812184)),
ImmutableSet.of(),
ImmutableMap.of("a", "b"),
ImmutableMap.of(),
"");

final byte[] serialize = spotify100ProtoSerializer.serialize(metric);

assertThat(serialize, any(byte[].class));
}

@Test(expected = NullPointerException.class)
public void testNullKeyException() throws Exception {
metric = new Metric(null,
0.0,
Date.from(Instant.ofEpochSecond(1542812184)),
ImmutableSet.of(),
ImmutableMap.of("a", "b"),
ImmutableMap.of(),
"");

spotify100ProtoSerializer.serialize(metric);
}

}
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -336,7 +342,31 @@
</dependencyManagement>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>

<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
Expand Down

0 comments on commit d9459c4

Please sign in to comment.