Search in sources :

Example 1 with HadoopTaskInput

use of org.apache.ignite.internal.processors.hadoop.HadoopTaskInput in project ignite by apache.

the class HadoopConcurrentHashMultimapSelftest method check.

private void check(HadoopConcurrentHashMultimap m, Multimap<Integer, Integer> mm, final Multimap<Integer, Integer> vis, HadoopTaskContext taskCtx) throws Exception {
    final HadoopTaskInput in = m.input(taskCtx);
    Map<Integer, Collection<Integer>> mmm = mm.asMap();
    int keys = 0;
    while (in.next()) {
        keys++;
        IntWritable k = (IntWritable) in.key();
        assertNotNull(k);
        Deque<Integer> vs = new LinkedList<>();
        Iterator<?> it = in.values();
        while (it.hasNext()) vs.addFirst(((IntWritable) it.next()).get());
        Collection<Integer> exp = mmm.get(k.get());
        assertEquals(exp, vs);
    }
    assertEquals(mmm.size(), keys);
    assertEquals(m.keys(), keys);
    X.println("keys: " + keys + " cap: " + m.capacity());
    // Check visitor.
    final byte[] buf = new byte[4];
    final GridDataInput dataInput = new GridUnsafeDataInput();
    m.visit(false, new HadoopConcurrentHashMultimap.Visitor() {

        /**
         */
        IntWritable key = new IntWritable();

        /**
         */
        IntWritable val = new IntWritable();

        @Override
        public void onKey(long keyPtr, int keySize) {
            read(keyPtr, keySize, key);
        }

        @Override
        public void onValue(long valPtr, int valSize) {
            read(valPtr, valSize, val);
            vis.put(key.get(), val.get());
        }

        private void read(long ptr, int size, Writable w) {
            assert size == 4 : size;
            GridUnsafe.copyOffheapHeap(ptr, buf, GridUnsafe.BYTE_ARR_OFF, size);
            dataInput.bytes(buf, size);
            try {
                w.readFields(dataInput);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    // X.println("vis: " + vis);
    assertEquals(mm, vis);
    in.close();
}
Also used : HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) Writable(org.apache.hadoop.io.Writable) IntWritable(org.apache.hadoop.io.IntWritable) IOException(java.io.IOException) LinkedList(java.util.LinkedList) GridUnsafeDataInput(org.apache.ignite.internal.util.io.GridUnsafeDataInput) Collection(java.util.Collection) GridDataInput(org.apache.ignite.internal.util.io.GridDataInput) HadoopConcurrentHashMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopConcurrentHashMultimap) IntWritable(org.apache.hadoop.io.IntWritable)

Example 2 with HadoopTaskInput

use of org.apache.ignite.internal.processors.hadoop.HadoopTaskInput in project ignite by apache.

the class HadoopConcurrentHashMultimapSelftest method testMultiThreaded.

/**
 * @throws Exception if failed.
 */
public void testMultiThreaded() throws Exception {
    GridUnsafeMemory mem = new GridUnsafeMemory(0);
    X.println("___ Started");
    Random rnd = new GridRandom();
    for (int i = 0; i < 20; i++) {
        HadoopJobInfo job = new JobInfo();
        final HadoopTaskContext taskCtx = new TaskContext();
        final HadoopConcurrentHashMultimap m = new HadoopConcurrentHashMultimap(job, mem, 16);
        final ConcurrentMap<Integer, Collection<Integer>> mm = new ConcurrentHashMap<>();
        X.println("___ MT");
        multithreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                X.println("___ TH in");
                Random rnd = new GridRandom();
                IntWritable key = new IntWritable();
                IntWritable val = new IntWritable();
                HadoopMultimap.Adder a = m.startAdding(taskCtx);
                for (int i = 0; i < 50000; i++) {
                    int k = rnd.nextInt(32000);
                    int v = rnd.nextInt();
                    key.set(k);
                    val.set(v);
                    a.write(key, val);
                    Collection<Integer> list = mm.get(k);
                    if (list == null) {
                        list = new ConcurrentLinkedQueue<>();
                        Collection<Integer> old = mm.putIfAbsent(k, list);
                        if (old != null)
                            list = old;
                    }
                    list.add(v);
                }
                a.close();
                X.println("___ TH out");
                return null;
            }
        }, 3 + rnd.nextInt(27));
        X.println("___ Check: " + m.capacity());
        assertEquals(mm.size(), m.keys());
        assertTrue(m.capacity() > 32000);
        HadoopTaskInput in = m.input(taskCtx);
        while (in.next()) {
            IntWritable key = (IntWritable) in.key();
            Iterator<?> valsIter = in.values();
            Collection<Integer> vals = mm.remove(key.get());
            assertNotNull(vals);
            while (valsIter.hasNext()) {
                IntWritable val = (IntWritable) valsIter.next();
                assertTrue(vals.remove(val.get()));
            }
            assertTrue(vals.isEmpty());
        }
        in.close();
        m.close();
        assertEquals(0, mem.allocatedSize());
    }
}
Also used : HadoopJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopJobInfo) HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) IOException(java.io.IOException) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) HadoopJobInfo(org.apache.ignite.internal.processors.hadoop.HadoopJobInfo) HadoopTaskContext(org.apache.ignite.internal.processors.hadoop.HadoopTaskContext) Collection(java.util.Collection) HadoopConcurrentHashMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopConcurrentHashMultimap) GridUnsafeMemory(org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) IntWritable(org.apache.hadoop.io.IntWritable)

Example 3 with HadoopTaskInput

use of org.apache.ignite.internal.processors.hadoop.HadoopTaskInput in project ignite by apache.

the class HadoopSkipListSelfTest method check.

/**
 * Check.
 * @param m The multimap.
 * @param mm The multimap storing expectations.
 * @param vis The multimap to store visitor results.
 * @param taskCtx The task context.
 * @throws Exception On error.
 */
private void check(HadoopMultimap m, Multimap<Integer, Integer> mm, final Multimap<Integer, Integer> vis, HadoopTaskContext taskCtx) throws Exception {
    final HadoopTaskInput in = m.input(taskCtx);
    Map<Integer, Collection<Integer>> mmm = mm.asMap();
    int keys = 0;
    int prevKey = Integer.MIN_VALUE;
    while (in.next()) {
        keys++;
        IntWritable k = (IntWritable) in.key();
        assertNotNull(k);
        assertTrue(k.get() > prevKey);
        prevKey = k.get();
        Deque<Integer> vs = new LinkedList<>();
        Iterator<?> it = in.values();
        while (it.hasNext()) vs.addFirst(((IntWritable) it.next()).get());
        Collection<Integer> exp = mmm.get(k.get());
        assertEquals(exp, vs);
    }
    assertEquals(mmm.size(), keys);
    // !        assertEquals(m.keys(), keys);
    // Check visitor.
    final byte[] buf = new byte[4];
    final GridDataInput dataInput = new GridUnsafeDataInput();
    m.visit(false, new HadoopMultimap.Visitor() {

        /**
         */
        IntWritable key = new IntWritable();

        /**
         */
        IntWritable val = new IntWritable();

        @Override
        public void onKey(long keyPtr, int keySize) {
            read(keyPtr, keySize, key);
        }

        @Override
        public void onValue(long valPtr, int valSize) {
            read(valPtr, valSize, val);
            vis.put(key.get(), val.get());
        }

        private void read(long ptr, int size, Writable w) {
            assert size == 4 : size;
            GridUnsafe.copyOffheapHeap(ptr, buf, GridUnsafe.BYTE_ARR_OFF, size);
            dataInput.bytes(buf, size);
            try {
                w.readFields(dataInput);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    // X.println("vis: " + vis);
    assertEquals(mm, vis);
    in.close();
}
Also used : HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) HadoopMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap) Writable(org.apache.hadoop.io.Writable) IntWritable(org.apache.hadoop.io.IntWritable) IOException(java.io.IOException) LinkedList(java.util.LinkedList) GridUnsafeDataInput(org.apache.ignite.internal.util.io.GridUnsafeDataInput) Collection(java.util.Collection) GridDataInput(org.apache.ignite.internal.util.io.GridDataInput) IntWritable(org.apache.hadoop.io.IntWritable)

Example 4 with HadoopTaskInput

use of org.apache.ignite.internal.processors.hadoop.HadoopTaskInput in project ignite by apache.

the class HadoopShuffleJob method input.

/**
 * @param taskCtx Task context.
 * @return Input.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
public HadoopTaskInput input(HadoopTaskContext taskCtx) throws IgniteCheckedException {
    switch(taskCtx.taskInfo().type()) {
        case REDUCE:
            int reducer = taskCtx.taskInfo().taskNumber();
            HadoopMultimap m = locMaps.get(reducer);
            if (m != null)
                return m.input(taskCtx);
            return new // Empty input.
            HadoopTaskInput() {

                @Override
                public boolean next() {
                    return false;
                }

                @Override
                public Object key() {
                    throw new IllegalStateException();
                }

                @Override
                public Iterator<?> values() {
                    throw new IllegalStateException();
                }

                @Override
                public void close() {
                // No-op.
                }
            };
        default:
            throw new IllegalStateException("Illegal type: " + taskCtx.taskInfo().type());
    }
}
Also used : HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) HadoopMultimap(org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap)

Example 5 with HadoopTaskInput

use of org.apache.ignite.internal.processors.hadoop.HadoopTaskInput in project ignite by apache.

the class HadoopSkipList method input.

/**
 * {@inheritDoc}
 */
@Override
public HadoopTaskInput input(HadoopTaskContext taskCtx) throws IgniteCheckedException {
    Input in = new Input(taskCtx);
    Comparator<Object> grpCmp = taskCtx.groupComparator();
    if (grpCmp != null)
        return new GroupedInput(grpCmp, in);
    return in;
}
Also used : HadoopTaskInput(org.apache.ignite.internal.processors.hadoop.HadoopTaskInput) DataInput(java.io.DataInput)

Aggregations

HadoopTaskInput (org.apache.ignite.internal.processors.hadoop.HadoopTaskInput)9 Collection (java.util.Collection)5 IntWritable (org.apache.hadoop.io.IntWritable)5 IOException (java.io.IOException)4 HadoopMultimap (org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopMultimap)3 LinkedList (java.util.LinkedList)2 Random (java.util.Random)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 Writable (org.apache.hadoop.io.Writable)2 HadoopJobInfo (org.apache.ignite.internal.processors.hadoop.HadoopJobInfo)2 HadoopTaskCancelledException (org.apache.ignite.internal.processors.hadoop.HadoopTaskCancelledException)2 HadoopTaskContext (org.apache.ignite.internal.processors.hadoop.HadoopTaskContext)2 HadoopConcurrentHashMultimap (org.apache.ignite.internal.processors.hadoop.shuffle.collections.HadoopConcurrentHashMultimap)2 GridRandom (org.apache.ignite.internal.util.GridRandom)2 GridDataInput (org.apache.ignite.internal.util.io.GridDataInput)2 GridUnsafeDataInput (org.apache.ignite.internal.util.io.GridUnsafeDataInput)2 GridUnsafeMemory (org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory)2 DataInput (java.io.DataInput)1 ArrayList (java.util.ArrayList)1