Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;

import org.apache.brooklyn.api.mgmt.rebind.RebindSupport;
import org.apache.brooklyn.api.objs.HighlightTuple;

/**
* Represents the state of an policy, so that it can be reconstructed (e.g. after restarting brooklyn).
Expand All @@ -32,4 +33,6 @@
public interface PolicyMemento extends Memento {

Map<String, Object> getConfig();

Map<String, HighlightTuple> getHighlights();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.brooklyn.api.objs;

import java.util.Map;

import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -50,4 +52,5 @@ public interface EntityAdjunct extends BrooklynObject {
*/
@Nullable String getUniqueTag();

Map<String, HighlightTuple> getHighlights();
}
81 changes: 81 additions & 0 deletions api/src/main/java/org/apache/brooklyn/api/objs/HighlightTuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.brooklyn.api.objs;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing java doc

public class HighlightTuple {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would AdjunctHighlight be a better name?

private String description;
private long time;
private String taskId;

//required for JSON de-serialisation
private HighlightTuple(){

}

public HighlightTuple(String description, long time, String taskId) {
this.description = description;
this.time = time;
this.taskId = taskId;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public long getTime() {
return time;
}

public void setTime(long time) {
this.time = time;
}

public String getTaskId() {
return taskId;
}

public void setTaskId(String taskId) {
this.taskId = taskId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

HighlightTuple that = (HighlightTuple) o;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Id personally use guava Objects.equalsas its already a dependency


if (time != that.time) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
return taskId != null ? taskId.equals(that.taskId) : that.taskId == null;
}

@Override
public int hashCode() {
int result = description != null ? description.hashCode() : 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above Objects.hashCode

result = 31 * result + (int) (time ^ (time >>> 32));
result = 31 * result + (taskId != null ? taskId.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,7 @@ protected Policy newPolicy(PolicyMemento memento) {
policy = policyFactory.constructPolicy(policyClazz);
FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", id), policy);
((AbstractPolicy)policy).setManagementContext(managementContext);
((AbstractPolicy)policy).setHighlights(memento.getHighlights());

} else {
LOG.warn("Deprecated rebind of policy without no-arg constructor; " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;

import org.apache.brooklyn.api.mgmt.rebind.mementos.PolicyMemento;
import org.apache.brooklyn.api.objs.HighlightTuple;
import org.apache.brooklyn.core.config.Sanitizer;

import com.google.common.base.MoreObjects;
Expand All @@ -42,22 +43,28 @@ public static Builder builder() {

public static class Builder extends AbstractMemento.Builder<Builder> {
protected Map<String,Object> config = Maps.newLinkedHashMap();
protected Map<String,HighlightTuple> highlights = Maps.newLinkedHashMap();

public Builder from(PolicyMemento other) {
super.from(other);
config.putAll(other.getConfig());
highlights.putAll(other.getHighlights());
return this;
}
public Builder config(Map<String,?> vals) {
config.putAll(vals); return this;
}
public Builder highlights(Map<String,HighlightTuple> vals) {
highlights.putAll(vals); return this;
}
public PolicyMemento build() {
return new BasicPolicyMemento(this);
}
}

private Map<String,Object> config;
private Map<String, Object> fields;
private Map<String, HighlightTuple> highlights;

@SuppressWarnings("unused") // For deserialisation
private BasicPolicyMemento() {}
Expand All @@ -66,6 +73,7 @@ private BasicPolicyMemento() {}
protected BasicPolicyMemento(Builder builder) {
super(builder);
config = toPersistedMap(builder.config);
highlights = toPersistedMap(builder.highlights);
}

@Deprecated
Expand All @@ -84,9 +92,14 @@ public Map<String, Object> getCustomFields() {
public Map<String, Object> getConfig() {
return fromPersistedMap(config);
}


@Override
public Map<String, HighlightTuple> getHighlights() {
return highlights;
}

@Override
protected MoreObjects.ToStringHelper newVerboseStringHelper() {
return super.newVerboseStringHelper().add("config", Sanitizer.sanitize(getConfig()));
return super.newVerboseStringHelper().add("config", Sanitizer.sanitize(getConfig())).add("highlights", highlights);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ public static PolicyMemento newPolicyMemento(Policy policy) {
Object value = configValueToPersistable(entry.getValue(), policy, key.getName());
builder.config.put(key.getName(), value);
}


builder.highlights(policy.getHighlights());

Map<String, Object> persistableFlags = MutableMap.<String, Object>builder()
.putAll(FlagUtils.getFieldsWithFlagsExcludingModifiers(policy, Modifier.STATIC ^ Modifier.TRANSIENT))
.remove("id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
Expand All @@ -36,6 +37,7 @@
import org.apache.brooklyn.api.objs.BrooklynObject;
import org.apache.brooklyn.api.objs.Configurable;
import org.apache.brooklyn.api.objs.EntityAdjunct;
import org.apache.brooklyn.api.objs.HighlightTuple;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.api.sensor.Sensor;
import org.apache.brooklyn.api.sensor.SensorEventListener;
Expand Down Expand Up @@ -109,6 +111,13 @@ public abstract class AbstractEntityAdjunct extends AbstractBrooklynObject imple
@SetFromFlag(value="uniqueTag")
protected String uniqueTag;

private Map<String, HighlightTuple> highlights = new HashMap<>();

public static String HIGHLIGHT_NAME_LAST_ACTION = "lastAction";
public static String HIGHLIGHT_NAME_LAST_CONFIRMATION= "lastConfirmation";
public static String HIGHLIGHT_NAME_LAST_VIOLATION= "lastViolation";
public static String HIGHLIGHT_NAME_TRIGGERS = "triggers";

public AbstractEntityAdjunct() {
this(Collections.emptyMap());
}
Expand All @@ -128,6 +137,10 @@ public AbstractEntityAdjunct(@SuppressWarnings("rawtypes") Map properties) {
}
}

protected void addHighlight(String name, HighlightTuple tuple) {
highlights.put(name, tuple);
}

/**
* @deprecated since 0.7.0; only used for legacy brooklyn types where constructor is called directly
*/
Expand Down Expand Up @@ -548,6 +561,23 @@ public void setUniqueTag(String uniqueTag) {
}
}

@Override
public Map<String, HighlightTuple> getHighlights() {
HashMap<String, HighlightTuple> highlightsToReturn = new HashMap<>();
highlightsToReturn.putAll(highlights);
return highlightsToReturn;
}

/**
* Should only be used for rebind
* @param highlights
*/
public void setHighlights(Map<String, HighlightTuple> highlights) {
if(highlights != null) {
this.highlights.putAll(highlights);
}
}

@Override
public String toString() {
return MoreObjects.toStringHelper(getClass()).omitNullValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Map;

import org.apache.brooklyn.api.objs.HighlightTuple;
import org.apache.brooklyn.api.policy.PolicySpec;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.BasicConfigKey;
Expand Down Expand Up @@ -70,6 +71,11 @@ protected <T> void doReconfigureConfig(ConfigKey<T> key, T val) {
}
}

//make visable for testing
@Override
protected void addHighlight(String name, HighlightTuple tuple) {
super.addHighlight(name, tuple);
}
}

@Test
Expand Down Expand Up @@ -106,4 +112,16 @@ public void testTagsFromSpec() throws Exception {
assertEquals(policy.getUniqueTag(), "x");
}

@Test
public void testHighlights() throws Exception {
MyPolicy policy = new MyPolicy();

HighlightTuple highlight = new HighlightTuple("TEST_DESCRIPTION", 123L, "456");
policy.addHighlight("testHighlightName", highlight);

Map<String, HighlightTuple> highlights = policy.getHighlights();

assertEquals(1, highlights.size());
assertEquals(highlight, highlights.get("testHighlightName"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@
package org.apache.brooklyn.policy.autoscaling;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.location.LocationSpec;
import org.apache.brooklyn.api.objs.HighlightTuple;
import org.apache.brooklyn.api.policy.Policy;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.core.entity.EntityAsserts;
import org.apache.brooklyn.core.entity.EntityInternal;
import org.apache.brooklyn.core.location.SimulatedLocation;
import org.apache.brooklyn.core.mgmt.rebind.RebindTestFixtureWithApp;
import org.apache.brooklyn.core.objs.AbstractEntityAdjunct;
import org.apache.brooklyn.core.sensor.BasicNotificationSensor;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.core.test.entity.TestApplication;
Expand Down Expand Up @@ -105,6 +110,7 @@ public void testRestoresAutoScalerConfig() throws Exception {
assertEquals(newPolicy.getConfig(AutoScalerPolicy.POOL_OK_SENSOR), POOL_OK_SENSOR);
assertEquals(newPolicy.getConfig(AutoScalerPolicy.MAX_SIZE_REACHED_SENSOR), MAX_SIZE_REACHED_SENSOR);
assertEquals(newPolicy.getConfig(AutoScalerPolicy.MAX_REACHED_NOTIFICATION_DELAY), Duration.of(7, TimeUnit.MILLISECONDS));
assertTrue(newPolicy.getHighlights().isEmpty());
}

@Test
Expand All @@ -131,4 +137,32 @@ public void testAutoScalerResizesAfterRebind() throws Exception {
((EntityInternal)newCluster).sensors().set(METRIC_SENSOR, 1);
EntityAsserts.assertGroupSizeEqualsEventually(newCluster, 1);
}

@Test
public void testAutoScalerHighlightAfterRebind() throws Exception {
origCluster.start(ImmutableList.of(origLoc));
origCluster.policies().add(AutoScalerPolicy.builder()
.name("myname")
.metric(METRIC_SENSOR)
.entityWithMetric(origCluster)
.metricUpperBound(10)
.metricLowerBound(100)
.minPoolSize(1)
.maxPoolSize(3)
.buildSpec());

Map<String, HighlightTuple> highlights = new HashMap<>();
highlights.put("testNameTask", new HighlightTuple("testDescription", 123L, "testTaskId"));


Policy originalPolicy = origCluster.policies().iterator().next();
((AbstractEntityAdjunct)originalPolicy).setHighlights(highlights);

TestApplication newApp = rebind();

DynamicCluster newCluster = (DynamicCluster) Iterables.getOnlyElement(newApp.getChildren());
AutoScalerPolicy newPolicy = (AutoScalerPolicy) Iterables.getOnlyElement(newCluster.policies());

assertEquals(originalPolicy.getHighlights(), newPolicy.getHighlights());
}
}
Loading