use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.
the class IgfsStreamsSelfTest method testCreateFileColocated.
/** @throws Exception If failed. */
public void testCreateFileColocated() throws Exception {
IgfsPath path = new IgfsPath("/colocated");
UUID uuid = UUID.randomUUID();
IgniteUuid affKey;
long idx = 0;
while (true) {
affKey = new IgniteUuid(uuid, idx);
if (grid(0).affinity(grid(0).igfsx("igfs").configuration().getDataCacheConfiguration().getName()).mapKeyToNode(affKey).id().equals(grid(0).localNode().id()))
break;
idx++;
}
try (IgfsOutputStream out = fs.create(path, 1024, true, affKey, 0, 1024, null)) {
// Write 5M, should be enough to test distribution.
for (int i = 0; i < 15; i++) out.write(new byte[1024 * 1024]);
}
IgfsFile info = fs.info(path);
Collection<IgfsBlockLocation> affNodes = fs.affinity(path, 0, info.length());
assertEquals(1, affNodes.size());
Collection<UUID> nodeIds = F.first(affNodes).nodeIds();
assertEquals(1, nodeIds.size());
assertEquals(grid(0).localNode().id(), F.first(nodeIds));
}
use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.
the class CacheUtils method sparseMap.
/**
* @param matrixUuid Matrix UUID.
* @param mapper Mapping {@link IgniteFunction}.
*/
public static <K, V> void sparseMap(IgniteUuid matrixUuid, IgniteFunction<Double, Double> mapper) {
foreach(SparseDistributedMatrixStorage.ML_CACHE_NAME, (CacheEntry<IgniteBiTuple<Integer, IgniteUuid>, Map<Integer, Double>> ce) -> {
IgniteBiTuple k = ce.entry().getKey();
Map<Integer, Double> v = ce.entry().getValue();
for (Map.Entry<Integer, Double> e : v.entrySet()) e.setValue(mapper.apply(e.getValue()));
ce.cache().put(k, v);
}, key -> key.get2().equals(matrixUuid));
}
use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.
the class KMeansDistributedClusterer method initClusterCenters.
/** Initialize cluster centers. */
private Vector[] initClusterCenters(SparseDistributedMatrix points, int k) {
// Initialize empty centers and point costs.
int ptsCount = points.rowSize();
// Initialize the first center to a random point.
Vector sample = localCopyOf(points.viewRow(rnd.nextInt(ptsCount)));
List<Vector> centers = new ArrayList<>();
List<Vector> newCenters = new ArrayList<>();
newCenters.add(sample);
centers.add(sample);
final ConcurrentHashMap<Integer, Double> costs = new ConcurrentHashMap<>();
// On each step, sample 2 * k points on average with probability proportional
// to their squared distance from the centers. Note that only distances between points
// and new centers are computed in each iteration.
int step = 0;
IgniteUuid uid = points.getUUID();
while (step < initSteps) {
// We assume here that costs can fit into memory of one node.
ConcurrentHashMap<Integer, Double> newCosts = getNewCosts(points, newCenters);
// Merge costs with new costs.
for (Integer ind : newCosts.keySet()) costs.merge(ind, newCosts.get(ind), Math::min);
double sumCosts = costs.values().stream().mapToDouble(Double::valueOf).sum();
newCenters = getNewCenters(k, costs, uid, sumCosts);
centers.addAll(newCenters);
step++;
}
List<Vector> distinctCenters = centers.stream().distinct().collect(Collectors.toList());
if (distinctCenters.size() <= k)
return distinctCenters.toArray(new Vector[] {});
else {
// Finally, we might have a set of more than k distinct candidate centers; weight each
// candidate by the number of points in the dataset mapping to it and run a local k-means++
// on the weighted centers to pick k of them
ConcurrentHashMap<Integer, Integer> centerInd2Weight = weightCenters(uid, distinctCenters);
List<Double> weights = new ArrayList<>(centerInd2Weight.size());
for (int i = 0; i < distinctCenters.size(); i++) weights.add(i, Double.valueOf(centerInd2Weight.getOrDefault(i, 0)));
DenseLocalOnHeapMatrix dCenters = MatrixUtil.fromList(distinctCenters, true);
return new KMeansLocalClusterer(getDistanceMeasure(), 30, seed).cluster(dCenters, k, weights).centers();
}
}
use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.
the class IgfsFragmentizerAbstractSelfTest method awaitFileFragmenting.
/**
* @param gridIdx Grid index.
* @param path Path to await.
* @throws Exception If failed.
*/
protected void awaitFileFragmenting(int gridIdx, IgfsPath path) throws Exception {
IgfsEx igfs = (IgfsEx) grid(gridIdx).fileSystem("igfs");
IgfsMetaManager meta = igfs.context().meta();
IgniteUuid fileId = meta.fileId(path);
if (fileId == null)
throw new IgfsPathNotFoundException("File not found: " + path);
IgfsEntryInfo fileInfo = meta.info(fileId);
do {
if (fileInfo == null)
throw new IgfsPathNotFoundException("File not found: " + path);
if (fileInfo.fileMap().ranges().isEmpty())
return;
U.sleep(100);
fileInfo = meta.info(fileId);
} while (true);
}
use of org.apache.ignite.lang.IgniteUuid in project ignite by apache.
the class IgfsMetaManagerSelfTest method mkdirsAndGetInfo.
private IgfsEntryInfo mkdirsAndGetInfo(String path) throws IgniteCheckedException {
IgfsPath p = path(path);
mgr.mkdirs(p, IgfsImpl.DFLT_DIR_META);
IgniteUuid id = mgr.fileId(p);
IgfsEntryInfo info = mgr.info(id);
assert info.isDirectory();
return info;
}
Aggregations