Redirecting varnish to new backend (emergency)

The following 3 articles can e used to redirect a varnish to another backend without restarting varnish and losing the existing cache.

 

1) Change VCL backend

 

Go to your vcl file (in my case /etc/varnish/default.vcl). Edit the backend param to point to another server. Also increase the timeouts since it will take longer than accessing localhost.

backend default {
.host = “backupserver.org”;
.port = “80″;
.connect_timeout = 120s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 60s;
}

2) Validate VCL script

See: http://maythesource.com/2013/01/18/easily-validating-your-varnish-cache-vcl-file-and-other-quick-tips/

 

3) Reload the VCL Script without restarting varnish

I utilized:

$ varnishadm -T localhost:81
vcl.load reload01 /etc/varnish/default.vcl
vcl.use reload01

Reference:  http://technosophos.com/2010/09/09/varnish-reloading-vcl-configuration-files-varnishadm.html

 

API-Axle: Tips, Tricks and Gotchas

Api-Axle

This is a brief post to help beginners with API-Axle. What is API-Axle? As described on their site:

ApiAxle is a proxy that sits on your network, in front of your API(s) and manages things that you shouldn’t have to, like rate limiting, authentication and analytics. It’s fast, open and easy to configure.

Here are some tips to help out with the initial instructions at:  http://apiaxle.com/docs/try-it-now/ .

Note: myapi in myapi.api.localhost should be replaced by your own api name.

Increase Timeout

The initial timeout (2 seconds) can result in errors because your backend API server takes time to reply to api-axle. In order to increase this you can use the endPointTimeout parameter.

Example (10 seconds):

axle> api "myapi" create endPoint="stream.site.org:80" endPointTimeout=10 
axle> key "1234" create 
axle> api myapi linkkey "1234"

More

Android: Circular ViewPager

Recently I was building an Android Application and needed a circular ViewPager. That is a ViewPager that will automatically move to the first page when you swipe the last. Apparently there is no Native Support for this, neither could I find any good implementation online.

So here is a simple but effective way to do this.

The idea is basically to apply the functionality to the PagerAdapter you will insert in the ViewPager. What you do is create a huge set of Pages (lets say 10000) and set the current page in the middle.

So here it is:

/**
 * Created by Chris Georgoulis on 8/3/2014.
 */
public class ImageAdapter extends PagerAdapter {

	public static final int PAGER_PAGES = 10000;
	public static final int PAGER_PAGES_MIDDLE = PAGER_PAGES / 2;

	private List drawables;

	public ImageAdapter(List drawables) {
		this.drawables = drawables;
	}

More

Create a Word Auto-Completer in C using the Trie Data Structure

So first of all a few words about our basic structure, the trie…

In computer science, a trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by prefixes), is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are normally not associated with every node, only with leaves and some inner nodes that correspond to keys of interest.

For example, in our case, our project involves alphabets with every inner node of the trie data structure representing a letter which means that each node has a unique path from the root (can have up to 26 children as the number of the Latin alphabet) that symbolizes a word.

 

In our project, in order to create our fancy auto-completer we will need three structures. Firstly we need the structure of the

inner nodes that store a character that represents the letter of this node, an array of 26 letters that will navigate through the next letter. Moreover to make our program more accurate we will use an array that stores the top leafs (top words) under this inner node and a leaf pointer that is going to show us that the path from the root until this inner node reflects a word. The leaf structure contains the frequency of this word (which is the parent of the leaf) and a pointer to the last letter of the word.

More

Java EE DAO Pattern

Java EE really makes our life easy when dealing with Database Management. Here is a basic DAO design pattern that I use in most of my projects:

public abstract class AbstractDao, ID extends Serializable> {

protected abstract EntityManager getEntityManager();

private Class entity;

public AbstractDao(Class entity) {
this.entity = entity;
}

public T find(ID id) {
return getEntityManager().find(entity, id);
}

public List findAll() {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()
.createQuery(entity);
cq.select(cq.from(entity));

return getEntityManager().createQuery(cq).getResultList();
}

public List findByPage(int pageNum, int pageSize) {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()
.createQuery(entity);
cq.select(cq.from(entity));

int firstResult = pageNum * pageSize;
return getEntityManager().createQuery(cq).setFirstResult(firstResult)
.setMaxResults(pageSize).getResultList();
}

public long count() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Long.class);
Root personRoot = cq.from(entity);

cq.select(cb.count(personRoot));

List list = getEntityManager().createQuery(cq).setMaxResults(1)
.getResultList();

long personsNumber = 0l;
if (!list.isEmpty()) {
personsNumber = list.get(0);
}
return personsNumber;
}

public T save(T entity) {
try {
if (entity.getId() == null) {
getEntityManager().persist(entity);
} else {
entity = getEntityManager().merge(entity);
}
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return entity;
}

public T saveAndFlush(T entity) {
try {
entity = save(entity);
flush();
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return entity;
}

public void flush() {
getEntityManager().flush();
}

public List saveAllAndFlush(Collection collection) {
List list = null;
try {
list = collection.parallelStream()
.map(this::save)
.collect(Collectors.toList());
flush();
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return list;
}

public boolean exists(ID id) {
return find(id) != null;
}

protected abstract Logger getLogger();
}

VBA: Read localstorage variable from Internet Explorer Object using temporary textfield

Here is a toy solution to access localstorage variables if they are not accessible directly.

Imagine we have test HTML webpage with HTML:

<html>
<body>
<script>
// Put the object into storage
localStorage.setItem('testObject', 'testString');

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
</script>
</body>
</html>

More

Mozilla has disallowed javascript: execution via the address bar

If you try running javascript code via the address bar (not not from a bookmark) it no longer works as mozilla has disabled it for 2 years now.

Trying the following:

javascript:alert("hello");

Causes the following error to appear in the error console:

Error: uncaught exception: ReferenceError: alert is not defined

More

Quickly find all php short-tags (<?= )

I found a webapplication running on linux where the previous developer had been using  shortcodes <? instead of the universally enabled <?php ( see http://stackoverflow.com/questions/1386620/php-echo-vs-php-short-tags for more info) .

Unfortunately this caused the php code to not function unless shortcuts are enabled.

More

Toy Example App that downloads Google Play APKs using Java and APKLeecher service

In the toy example below everything is done with string manipulation to extract the actual APK URL and proceed with the download.
The following can be improved by using a DOM parser and searching for the specific elements of interest.

More

How to avoid git conflicts when working with a team?

A new team member started working on the same project as me the last week.
After checking their first commit, I noticed they mixed refactoring with eclipse auto-formatting. What made this an even worse  transgression was that they have a custom maximum character width defined in their editor.  I am not against aggressively formatting or refactoring code – except when it is related to critical production code.

GIT diff unfortunately cannot handle these kinds of source refactoring changes and lists the whole file as changed. See: http://stackoverflow.com/questions/21897386/git-diff-ignore-all-linefeeds-between-revisions

After reading I came across the following gem on stackoverflow (How to avoid git conflicts when working with a team? from User Christopher) which I quote/steal from.

Ask your team three questions:

  1. Do we enforce whitespace conventions?automatically finalize parameters.
  2. Do we generate textual build artifacts? For example, do we minify js, generate css rules from .sass or .scss files, or build xml configurations on the fly? Do we check them in?[...]

These three things cause the vast majority of our collective conflict pain:

Versioning Pain

More