-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from chrisdennis/offheap-resource
- Loading branch information
Showing
16 changed files
with
1,010 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.terracotta</groupId> | ||
<artifactId>platform-root</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>offheap-resource</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<!-- This is dep is probably wrong --> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.7.18</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.terracotta.internal</groupId> | ||
<artifactId>tc-config-parser</artifactId> | ||
<version>10.0-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<version>1.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-toolchains-plugin</artifactId> | ||
<version>1.1</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>toolchain</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<toolchains> | ||
<jdk> | ||
<version>1.8</version> | ||
</jdk> | ||
</toolchains> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
<version>2.10.1</version> | ||
<configuration> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>attach-javadocs</id> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>jaxb2-maven-plugin</artifactId> | ||
<version>2.2</version> | ||
<executions> | ||
<execution> | ||
<id>xjc</id> | ||
<goals> | ||
<goal>xjc</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<packageName>org.terracotta.offheapresource.config</packageName> | ||
<sources> | ||
<source>src/main/resources/offheap-resource.xsd</source> | ||
</sources> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
93 changes: 93 additions & 0 deletions
93
offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResource.java
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,93 @@ | ||
/* | ||
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license. | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for | ||
* the specific language governing rights and limitations under the License. | ||
* | ||
* The Covered Software is OffHeap Resource. | ||
* | ||
* The Initial Developer of the Covered Software is | ||
* Terracotta, Inc., a Software AG company | ||
*/ | ||
|
||
package org.terracotta.offheapresource; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Represents an offheap resource, providing a reservation system that can be | ||
* used to control the combined memory usage of participating consumers. | ||
* <p> | ||
* Reservation and release calls perform no allocations, and therefore rely on | ||
* the cooperation of callers to achieve control over the 'real' resource usage. | ||
*/ | ||
public class OffHeapResource { | ||
|
||
private final AtomicLong remaining; | ||
|
||
/** | ||
* Creates a resource of the given initial size. | ||
* | ||
* @param size size of the resource | ||
* @throws IllegalArgumentException if the size is negative | ||
*/ | ||
OffHeapResource(long size) throws IllegalArgumentException { | ||
if (size < 0) { | ||
throw new IllegalArgumentException("Resource size cannot be negative"); | ||
} else { | ||
this.remaining = new AtomicLong(size); | ||
} | ||
} | ||
|
||
/** | ||
* Reserves the given amount of this resource. | ||
* <p> | ||
* This method <em>performs no allocation</em>. It is simply a reservation | ||
* that the consumer agrees to bind by the result of. A {@code false} return | ||
* should mean the caller refrains from performing any associated allocation. | ||
* | ||
* @param size reservation size | ||
* @return {@code true} if the reservation succeeded | ||
* @throws IllegalArgumentException if the reservation size is negative | ||
*/ | ||
public boolean reserve(long size) throws IllegalArgumentException { | ||
if (size < 0) { | ||
throw new IllegalArgumentException("Reservation size cannot be negative"); | ||
} else { | ||
for (long current = remaining.get(); current >= size; current = remaining.get()) { | ||
if (remaining.compareAndSet(current, current - size)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Releases the given amount of resource back to this pool. | ||
* | ||
* @param size release size | ||
* @throws IllegalArgumentException if the release size is negative | ||
*/ | ||
public void release(long size) throws IllegalArgumentException { | ||
if (size < 0) { | ||
throw new IllegalArgumentException("Released size cannot be negative"); | ||
} else { | ||
remaining.addAndGet(size); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the size of the remaining resource that can be reserved. | ||
* | ||
* @return the remaining resource size | ||
*/ | ||
public long available() { | ||
return remaining.get(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...urce/src/main/java/org/terracotta/offheapresource/OffHeapResourceConfigurationParser.java
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,70 @@ | ||
/* | ||
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license. | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for | ||
* the specific language governing rights and limitations under the License. | ||
* | ||
* The Covered Software is OffHeap Resource. | ||
* | ||
* The Initial Developer of the Covered Software is | ||
* Terracotta, Inc., a Software AG company | ||
*/ | ||
|
||
package org.terracotta.offheapresource; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import javax.xml.XMLConstants; | ||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBElement; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Unmarshaller; | ||
import javax.xml.transform.Source; | ||
import javax.xml.transform.stream.StreamSource; | ||
import javax.xml.validation.SchemaFactory; | ||
import org.terracotta.config.service.ServiceConfigParser; | ||
import org.terracotta.entity.ServiceProviderConfiguration; | ||
import org.w3c.dom.Element; | ||
|
||
import org.terracotta.offheapresource.config.OffheapResourcesType; | ||
import org.xml.sax.SAXException; | ||
|
||
public class OffHeapResourceConfigurationParser implements ServiceConfigParser { | ||
|
||
private static final URL XML_SCHEMA = OffHeapResourceConfigurationParser.class.getResource("/offheap-resource.xsd"); | ||
private static final URI NAMESPACE = URI.create("http://www.terracotta.org/config/offheap-resource"); | ||
|
||
@Override | ||
public Source getXmlSchema() throws IOException { | ||
return new StreamSource(XML_SCHEMA.openStream()); | ||
} | ||
|
||
@Override | ||
public URI getNamespace() { | ||
return NAMESPACE; | ||
} | ||
|
||
@Override | ||
public ServiceProviderConfiguration parse(Element elmnt, String string) { | ||
try { | ||
JAXBContext jaxbContext = JAXBContext.newInstance(OffheapResourcesType.class.getPackage().getName(), OffHeapResourceConfigurationParser.class.getClassLoader()); | ||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | ||
unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(getXmlSchema())); | ||
JAXBElement<OffheapResourcesType> parsed = (JAXBElement<OffheapResourcesType>) unmarshaller.unmarshal(elmnt); | ||
return new OffHeapResourcesConfiguration(parsed.getValue()); | ||
} catch (JAXBException e) { | ||
throw new IllegalArgumentException(e); | ||
} catch (SAXException e) { | ||
throw new AssertionError(e); | ||
} catch (IOException e) { | ||
throw new AssertionError(e); | ||
} | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
offheap-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourceIdentifier.java
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,56 @@ | ||
/* | ||
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license. | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for | ||
* the specific language governing rights and limitations under the License. | ||
* | ||
* The Covered Software is OffHeap Resource. | ||
* | ||
* The Initial Developer of the Covered Software is | ||
* Terracotta, Inc., a Software AG company | ||
*/ | ||
|
||
package org.terracotta.offheapresource; | ||
|
||
import org.terracotta.entity.ServiceConfiguration; | ||
|
||
/** | ||
* | ||
* @author cdennis | ||
*/ | ||
public final class OffHeapResourceIdentifier implements ServiceConfiguration<OffHeapResource> { | ||
|
||
private final String name; | ||
|
||
public static OffHeapResourceIdentifier identifier(String name) { | ||
return new OffHeapResourceIdentifier(name); | ||
} | ||
|
||
private OffHeapResourceIdentifier(String name) { | ||
if (name == null) { | ||
throw new NullPointerException("Name cannot be null"); | ||
} else { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return name.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return (obj instanceof OffHeapResourceIdentifier) && name.equals(((OffHeapResourceIdentifier) obj).name); | ||
} | ||
|
||
@Override | ||
public Class<OffHeapResource> getServiceType() { | ||
return OffHeapResource.class; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...-resource/src/main/java/org/terracotta/offheapresource/OffHeapResourcesConfiguration.java
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,43 @@ | ||
/* | ||
* The contents of this file are subject to the Terracotta Public 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://terracotta.org/legal/terracotta-public-license. | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for | ||
* the specific language governing rights and limitations under the License. | ||
* | ||
* The Covered Software is OffHeap Resource. | ||
* | ||
* The Initial Developer of the Covered Software is | ||
* Terracotta, Inc., a Software AG company | ||
*/ | ||
|
||
package org.terracotta.offheapresource; | ||
|
||
import java.util.Collection; | ||
import org.terracotta.entity.ServiceProvider; | ||
import org.terracotta.entity.ServiceProviderConfiguration; | ||
import org.terracotta.offheapresource.config.OffheapResourcesType; | ||
import org.terracotta.offheapresource.config.ResourceType; | ||
|
||
class OffHeapResourcesConfiguration implements ServiceProviderConfiguration { | ||
|
||
private final OffheapResourcesType xmlConfig; | ||
|
||
OffHeapResourcesConfiguration(OffheapResourcesType xmlConfig) { | ||
this.xmlConfig = xmlConfig; | ||
} | ||
|
||
public Collection<ResourceType> getResources() { | ||
return xmlConfig.getResource(); | ||
} | ||
|
||
@Override | ||
public Class<? extends ServiceProvider> getServiceProviderType() { | ||
return OffHeapResourcesProvider.class; | ||
} | ||
|
||
} |
Oops, something went wrong.