Skip to content

Commit

Permalink
[FLINK-6434] [flip6] Introduce SlotRequestID for SlotPool#allocateSlot
Browse files Browse the repository at this point in the history
We have to distinguish between the slot request and the slot allocation because
slot allocations are reused across multiple slot requests. Therefore we cannot use
the AllocationID to identify individual slot requests which might be cancelled.
Introducing a separate SlotRequestID solves the problem.
  • Loading branch information
tillrohrmann committed Nov 10, 2017
1 parent 2f9eb51 commit 56aefcd
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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.flink.runtime.instance;

import org.apache.flink.api.java.tuple.Tuple2;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

/**
* Map which stores values under two different indices.
*
* @param <A> Type of key A
* @param <B> Type of key B
* @param <V> Type of the value
*/
public class DualKeyMap<A, B, V> {

private final HashMap<A, Tuple2<B, V>> aMap;

private final HashMap<B, A> bMap;

private transient Collection<V> values;

public DualKeyMap(int initialCapacity) {
this.aMap = new HashMap<>(initialCapacity);
this.bMap = new HashMap<>(initialCapacity);
}

public int size() {
return aMap.size();
}

public V getKeyA(A aKey) {
final Tuple2<B, V> value = aMap.get(aKey);

if (value != null) {
return value.f1;
} else {
return null;
}
}

public V getKeyB(B bKey) {
final A aKey = bMap.get(bKey);

if (aKey != null) {
return aMap.get(aKey).f1;
} else {
return null;
}
}

public V put(A aKey, B bKey, V value) {
Tuple2<B, V> aValue = aMap.put(aKey, Tuple2.of(bKey, value));
bMap.put(bKey, aKey);

if (aValue != null) {
return aValue.f1;
} else {
return null;
}
}

public boolean containsKeyA(A aKey) {
return aMap.containsKey(aKey);
}

public boolean containsKeyB(B bKey) {
return bMap.containsKey(bKey);
}

public V removeKeyA(A aKey) {
Tuple2<B, V> aValue = aMap.remove(aKey);

if (aValue != null) {
bMap.remove(aValue.f0);
return aValue.f1;
} else {
return null;
}
}

public V removeKeyB(B bKey) {
A aKey = bMap.remove(bKey);

if (aKey != null) {
Tuple2<B, V> aValue = aMap.remove(aKey);
if (aValue != null) {
return aValue.f1;
} else {
return null;
}
} else {
return null;
}
}

public Collection<V> values() {
Collection<V> vs = values;

if (vs == null) {
vs = new Values();
values = vs;
}

return vs;
}

public void clear() {
aMap.clear();
bMap.clear();
}

private final class Values extends AbstractCollection<V> {

@Override
public Iterator<V> iterator() {
return new ValueIterator();
}

@Override
public int size() {
return aMap.size();
}
}

private final class ValueIterator implements Iterator<V> {

private final Iterator<Tuple2<B, V>> iterator = aMap.values().iterator();

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public V next() {
Tuple2<B, V> value = iterator.next();

return value.f1;
}
}
}
Loading

0 comments on commit 56aefcd

Please sign in to comment.